File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 60049
    
  65

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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://aspose.com/file-tools
 
subject: Problem in using BigDecimal values in for Loop
 
Similar Threads
Mod operation gives different output
How to save the double value in a customized format, such as keeping 6 digits only
regarding bigdecimal.divide problem
How NOT to convert dollar to cents
BigDecimal issue