• 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Rounding decimal

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I want to round the decimal value 5.345 up to 5.35. How is that possible using java.math.BigDecimal?

Using the follow code does not round up the last decimal point:

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Check the program.copied the program from roseindia
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but the method below also turns the value "5.345f" to "5.34f".

IsnĀ“t it normal to round up the value "0.45" to "0.5" instead of "0.4"?

sri ramvaithiyanathan wrote:
Check the program.copied the program from roseindia

 
Sheriff
Posts: 22696
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is the floating point values. Check out point 20 of the Beginner's FAQ.

In short, toBePaidTotalAmount is already slightly off directly after declaring. As a result the entire BigDecimal calculation is off. Use the String constructor instead: Now all floats and BigDecimals will print as 5.35.
 
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That method with floats doesn't help when you want BigDecimals. Also with floats, the three operations will introduce imprecisions.

Go through BigDecimal and look for methods like divide(), setScale() and round(). Also look at the RoundingMode enum, which you will find a link to in a divide method. You can encapsulate rounding and precision in one object called MathContext, also linked to from BigDecimal. . . . At least I think that code will work

Note the subtle difference between the round and setScale methods. And always remember BigDecimal is immutable; if you want to use the results you have to assign them to a BigDecimal reference.
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what I was taught in school (back in the stone age when we only had paper and pencil) was that if the last digit was a '5', you rounded to make it even. so 0.25 would round to 0.2, but 0.35 would round up to 0.4. The idea the teacher gave us was that this way, you are rounding up half the time, and rounding down half the time, so they balance out.

It sounded like crap to me, but that's what we were taught.
 
Campbell Ritchie
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends which country you are in. In most English-speaking countries we were taught "half-up" and think it means "half-away-from-zero", which you can find under RoundingMode as HALF_UP.

There is no absolute or right way to round halves: should you round up, down, towards 0, away from 0, towards an odd number, towards an even number, or even randomly? You will find a bit about IEEE-754R here, but I haven't read enough to know the rationale behind their choosing a particular rounding mode (they actually recommend half-towards-even). I believe the rationale of rounding one way half the time and the other way the rest is the correct explanation, but I don't know whether it is a better explanation than any of the other rounding modes. If there is no absolutely right way to round numbers, then we cannot consider the other ways as absolutely wrong.

And it definitely wasn't stone age when I was at school with paper and pencil (and I am probably older than you ). We thought we were modern.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks it works ;)

Rob Spoor wrote:The problem is the floating point values. Check out point 20 of the Beginner's FAQ.

In short, toBePaidTotalAmount is already slightly off directly after declaring. As a result the entire BigDecimal calculation is off. Use the String constructor instead: Now all floats and BigDecimals will print as 5.35.

 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic