I have a situation where I need to dynamically mask my decimals up to 2 decimal places. The user can only type in whole numbers. Examples:
- If the user types in 5, the output will be .05
- If the user types in 57, the output will be .57
- If the user types in 157, the output will be 1.57
How can I achieve this? I was not able to find a masking format that would allow this, so do I need to write my own? If so, any tips?
Here's what I have so far:
import java.text.DecimalFormat;
public class MyClass {
public static void main(String args[]) {
DecimalFormat myFormat = new DecimalFormat(".##");
System.out.println(myFormat.format(5)); // Expecting .05
System.out.println(myFormat.format(57)); // Expecting .57
System.out.println(myFormat.format(157)); // Expecting 1.57
}
}