programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How to keep doubleing a number

Ranch Hand
Posts: 43
Need help. This should be simple, but i can not seem to get it.
I want to take a double like 0.01 and then times it by 2 for each output.
like this....
0.01
0.02
0.04
0.08
0.16
Do i use a loop of if, else?

Thank you.

Greenhorn
Posts: 11

Adam Burda wrote:Need help. This should be simple, but i can not seem to get it.
I want to take a double like 0.01 and then times it by 2 for each output.
like this....
0.01
0.02
0.04
0.08
0.16
Do i use a loop of if, else?

Thank you.

You can use a loop like:

Ranch Hand
Posts: 43
Thank you. I feel like DUH....

Bartender
Posts: 4568
9
• 1
If you need to do something more than once, that suggests a loop. So then the question is which kind? If you know in advance how many times, then a for loop is the best fit. If you need to do it until a condition is met, then that's what while and do/while loops are for.

Bartender
Posts: 10575
66

Adam Burda wrote:I want to take a double like 0.01 and then times it by 2 for each output.

Just to add to the other good advice, you should be careful with this sort of thing when using double.

For specifically multiplying by 2, you're probably OK; but you may run into difficulties with other multipliers.

The reason is that 0.01 does NOT equal 0.01 - at least, not exactly.

For more information, I suggest you read this or this. The first is "the Bible", but the second is probably a bit easier to understand.

Winston