How to number formating with Java in various scenarios:
- if the value is 0,then output will be zero.
- if the value 1,then output will be 1.
- if the value is 1.2,then output will be 1.20.
- if the value is 1.20,then output will be 1.20.
So it means if the input value has decimals then i have to apply numberformat to two decimal places otherwise not required.
One
DecimalFormatterisn’t going to work for the case of no decimal and the case of two decimal places.Here’s some code that meets all 4 of your conditions:
Edited to add: For jambjo, a version that manipulates the String after the
DecimalFormatter.