I get an input from the user. I want to print it in this format:
0#.##
or
##.##
how do I do this in java?
Today I'm using:
padWithZeroRightToPeriod(product.formats[index],Float.parseFloat(currentPrice));
and
private String padWithZeroRightToPeriod(String serverFormat, float unformattedNumber) {
int nDigits = getNumberOfDigitsAfterPeriod(serverFormat);
String floatingFormat = "%4." + nDigits + "f";
String formattedPrice = String.format(floatingFormat, unformattedNumber);
return formattedPrice
}
but it converts 08.56 to 8.56.
For two decimal places, and at least two whole digits, with leading zeroes:
String floatingFormat = "%05.2f";
0 pads the string with zeros instead of spaces.5 is the minimum total field width, including the whole, fractional and decimal point parts.2 is the exact number of decimal places to be printed.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With