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

后端 未结 4 1652
伪装坚强ぢ
伪装坚强ぢ 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:45

    Adapted from Customizing Formats,

      public void localizedFormat(double value,Locale loc ) {
    
      NumberFormat nf = NumberFormat.getNumberInstance(loc);
      DecimalFormat df = (DecimalFormat)nf;
      df.applyPattern("###,###.00");
      String output = df.format(value);
    
      }
    

    This function should give you the required formatting.

提交回复
热议问题