aspose file tools*
The moose likes Beginning Java and the fly likes DecimalFormat not rounding with HALF_EVEN mode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DecimalFormat not rounding with HALF_EVEN mode" Watch "DecimalFormat not rounding with HALF_EVEN mode" New topic
Author

DecimalFormat not rounding with HALF_EVEN mode

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316


The ouput is 500. It means it just simply truncates the digits after decimal. How do I use the Decimal pattern like
"###0;-###0" to indicate it has no decimal but maintain an half-even rounding mode ? I read the DecimalFormat API and it says its default rounding mode is half-even. But it seems this rounding is not applied here.

Please help.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8229
    
  23

Raj Ohadi wrote:I read the DecimalFormat API and it says its default rounding mode is half-even.

I'm an old printf() fan (String.format() works the same way BTW), and with that you'd use:but I can't help much with DecimalFormat because I'm no expert. I suspect it has far more options; with printf()/String.format() you have to accept its default rounding (whatever that is; probably half-even too).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Winston Gutkowski wrote: . . its default rounding (whatever that is; probably half-even too).

Winston
I think it’s half away from zero, but I’m not certain.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
I have checked in the Formatter class; for %e it says[quot]ethe value will be rounded using the round half up algorithm.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: DecimalFormat not rounding with HALF_EVEN mode