How to format double value for a given locale and number of decimal places?

后端 未结 4 1658
伪装坚强ぢ
伪装坚强ぢ 2020-12-17 15:27

How can I use NumberFormat to format a double value for a given Locale (default locale is sufficient) and for a given number of decima

4条回答
  •  悲哀的现实
    2020-12-17 15:53

    converts a double value to a double value with any number of digits after decimal. I have created this method in a Utility class to access it through out the project.

    public static double convertToDecimal(double doubleValue, int numOfDecimals)  {
            BigDecimal bd = new BigDecimal(doubleValue);
            bd = bd.setScale(numOfDecimals, BigDecimal.ROUND_HALF_UP);
            return bd.doubleValue();
        }
    

提交回复
热议问题