I have double
values that I would like to convert to String
values with the following format restrictions:
number_of_fraction_digits = max (0, 5 - number_of_integer_digits)
Essentially, I want, if possible, to keep the number of digits to 5, rounding decimal digits, if necessary. For instance:
float String
-------------------------
eleven
100 100
100000 100000
99999 99999
99999.99 99999
9999.99 9999.9
999.99 999.99
23.34324 23.343
I have studied using DecimalFormat , but as far as I can tell, it doesn’t quite do what I need.
It allows you to set the maximum number of decimal digits with setMaximumFractionDigits()
, but as far as I can tell, I will have to calculate the number of integer digits and perform the above calculation.
So, the main question is whether there is a beautiful, clean, built-in way to format numbers in this way.
source share