Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Need Help in BigDecimal

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell and Tony for the links. It is very helpful.
 
Campbell Ritchie
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
A timing clock, fuse wire, high explosives and a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic