java - Why does HALF_UP sometimes round down with double? -
the following code:
double doublevalue = 1713.6; float floatvalue = 1713.6f; string fs = "%-9s : %-7s %-7s\n"; system.out.printf( fs, "", "double", "float" ); decimalformat format = new decimalformat("#0"); system.out.printf( fs, "tostring", string.valueof( doublevalue ), string.valueof( floatvalue ) ); format.setroundingmode( roundingmode.down ); system.out.printf( fs, "down", format.format( doublevalue ), format.format( floatvalue ) ); format.setroundingmode( roundingmode.half_down ); system.out.printf( fs, "half_down", format.format( doublevalue ), format.format( floatvalue ) ); format.setroundingmode( roundingmode.half_up ); system.out.printf( fs, "half_up", format.format( doublevalue ), format.format( floatvalue ) ); format.setroundingmode( roundingmode.up ); system.out.printf( fs, "up", format.format( doublevalue ), format.format( floatvalue ) );
produces result (live code):
: double float tostring : 1713.6 1713.6 down : 1713 1713 half_down : 1714 1714 half_up : 1713 1714 <--- notice line : 1714 1714
i know numbers cannot represented floating-point numbers. actual floating-point representation 1713.6 1713.5999755859375 (see this page).
but why half_up round down in case?
using java 1.8u25
there bug in java 8 regarding numberformat , roundingmod half_up see 8039915. fixed 8u40 (release notes).
Comments
Post a Comment