2

I can able to find a answer for limiting numbers after decimal. But if my count after decimal is not fixed, then I have to do something for that. I just want to pass the count for limiting numbers after decimal. The count will differ everytime. So in this case what I want to do? Anybody help me to find a better solution.

Sandhiya
  • 339
  • 2
  • 14

1 Answers1

2
    public static String format(Double d, int decimalPlace) {
        String pattern = "0." + String.format("%0" + decimalPlace + "d", 0);
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(d);
    }

Example

  • format(12.345124,1) -> 12.3
  • format(12.345124,2) -> 12.34
  • format(12.345124,3) -> 12.345
  • format(12.345124,4) -> 12.3451