I need to to show decimal places of a float value only when it is different from zeros.
How to format in this way using regex or DecimalFormat?
To display decimals the way you desire, use the following snippet:
// This is to show symbol . instead of ,
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US);
// Define the maximum number of decimals (number of symbols #)
DecimalFormat df = new DecimalFormat("#.##########", otherSymbols);
// type: double
System.out.println(df.format(5.0)); // writes 5
System.out.println(df.format(7.3)); // writes 7.3
System.out.println(df.format(9.000003)); // writes 9.000003
// type: float
System.out.println(df.format(9.000003f)); // writes 9.000002861
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