I need to print doubles rounded to a variable number of digits after decimal point so that insignificant zeros are not printed. E.g. I want both numbers 123 and 123.4 to be printed as is -- the first one without decimal point and the second one with it. If there are more than significant digits after decimal point then the number should be truncated to six digits.
What I describe is the default behavior of boost::locale::format if you print numbers as boost::locale::format("{1,number}") % double_value
. What I need is to do the same in Java, desirably using String.format. I tried %f
and %g
, but both of them print at least six digits.
All numbers I want to format are in range [0.1, 30000] so turning to scientific form should not be a pain here.