aspose file tools
The moose likes Beginning Java and the fly likes Problem in using BigDecimal values in for Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Problem in using BigDecimal values in for Loop" Watch "Problem in using BigDecimal values in for Loop" New topic
Author

Problem in using BigDecimal values in for Loop

Nagendra Batchu
Ranch Hand

Joined: Jan 23, 2008
Posts: 32
Hi All,

BigDecimal bd5=new BigDecimal("89999999999994552010");
BigDecimal bd6=new BigDecimal("89999999999994552020");

how to iterate it from starting bd5 to end of bd6

for(BigDecimal bd51=bd5;bd5.max(bd6);bd51.add(one)){

}

i had used this which is not working ... could any one help me in solving this problem
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
try this code:


your code is wrong in multiple ways:

1. bd5.max(bd6) returns BigDecimal, it does not compare them, you can compare them with bd51.compareTo(bd6)
2. you cannot use bd5 in the comparison, unless you change bd5 or bd6. Otherwise you end up with infinite loop. Though you can change them inside the body of the loop somewhere, then you good to go with this comparison (remember to use compareTo() , not max())
3. BigDecimal is immutable, so bd51.add(BigDecimal.ONE) returns new BigDecimal, but does not change the bd51 unless you assign it a new value yourself.

[SCJP 6.0]
Nagendra Batchu
Ranch Hand

Joined: Jan 23, 2008
Posts: 32
Thanks a lot...!!!
Nagendra Batchu
Ranch Hand

Joined: Jan 23, 2008
Posts: 32
Thanks a lot...!!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56233
    
  13

Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets.

For more information, please read this.

This post has been moved to a more appropriate forum.

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32712
    
    4
You should only use integer numbers as the counter in a for loop. I presume this example was justified under the heading of "I just wanted to see what happens if . . ."
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problem in using BigDecimal values in for Loop
 
Similar Threads
regarding bigdecimal.divide problem
BigDecimal issue
How NOT to convert dollar to cents
Mod operation gives different output
How to save the double value in a customized format, such as keeping 6 digits only