Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help in BigDecimal  RSS feed

 
changu mani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
I need your help in solving this problem.

I've a code as shown below. If User enters an amount of 10,000,000 instAmt goes as 1.0E+7. I want the amount to be sent as 10,000,000.00 only to the third party. It should not even go as 10,000,000.0000000123


Please let me know how it should be handled.

Thanks in adavncae
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start you shouldn't be using doubles to hold your currency value at any stage of the process. They can't represent every possible value and will introduce rounding errors.
I suggest you read a tutorial on handling currency values before you go any further, such as http://www.javapractices.com/topic/TopicAction.do?Id=13
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That’s a good tutorial. Lots of people have difficulties with BigDecimal; look at this thread, and look for other links there.
 
changu mani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell and Tony for the links. It is very helpful.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
Did you get your program to work?
 
changu mani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,
I've done this way.

new BigDecimal(speiAmnt).toPlainString();

It is working the way I expected now. If Customer enters 10,000,000. It goes as 10000000 without getting converted to scientific notation. Earlier with toString() method it was going as 1.0E+7. Now the third party interface is not throwing error saying invalid amount.

Thank you.

 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is worth learning about the % tags, which allow you to format Strings. You can find out about the % tags here and here and here. There are String methods which use the % tags.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changu mani wrote:I've done this way.
new BigDecimal(speiAmnt).toPlainString();
It is working the way I expected now. If Customer enters 10,000,000. It goes as 10000000 without getting converted to scientific notation. Earlier with toString() method it was going as 1.0E+7. Now the third party interface is not throwing error saying invalid amount.

Just FYI, that is one of the very rare cases where Java has changed the way a method works. BigDecimal.toString() used to work like toPlainString() does now; but it was changed in version 5.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!