【问题标题】:How can I format int numbers in java?如何在 java 中格式化 int 数字?
【发布时间】:2016-11-09 16:39:47
【问题描述】:

我正在尝试将此格式应用于给定的整数 #´###,### 我尝试使用 DecimalFormat class 但是当我需要两个 accute accent 用于数百万和逗号用于数千时,它只允许使用一个分组分隔符.

所以最后我可以用这种方式格式化1,000 或百万1´000,000 之类的值

【问题讨论】:

  • @Roshan 该问题中的解决方案使用substring()replace(),这对于电话“号码”可能没问题,但在处理实际号码时这不是最好的主意(比如钱)。 OP 需要考虑负数、不带小数点的数字等。
  • 谢谢@Roshan!

标签: java string-formatting


【解决方案1】:

我总是更喜欢使用 String.format,但我不确定是否有一个可以像这样格式化数字的 Locale。不过,这里有一些代码可以完成这项工作。

// Not sure if you wanted to start with a number or a string. Adjust accordingly
String stringValue = "1000000";
float floatValue = Float.valueOf(stringValue);

// Format the string to a known format
String formattedValue = String.format(Locale.US, "%,.2f", floatValue);

// Split the string on the separator
String[] parts = formattedValue.split(",");

// Put the parts back together with the special separators
String specialFormattedString = "";
int partsRemaining = parts.length;
for(int i=0;i<parts.length;i++)
{
    specialFormattedString += parts[i];
    partsRemaining--;
    if(partsRemaining > 1)
        specialFormattedString += "`";
    else if(partsRemaining == 1)
        specialFormattedString += ",";
}

【讨论】:

    【解决方案2】:

    我发现@Roshan 在 cmets 中提供的链接很有用,此解决方案使用正则表达式和 replaceFirst 方法

    public static String audienceFormat(int number) {
        String value = String.valueOf(number);
    
        if (value.length() > 6) {
                value = value.replaceFirst("(\\d{1,3})(\\d{3})(\\d{3})", "$1\u00B4$2,$3");
            } else if (value.length() >=5 && value.length() <= 6) {
                value = value.replaceFirst("(\\d{2,3})(\\d{3})", "$1,$2");
            }  else {
                value = value.replaceFirst("(\\d{1})(\\d+)", "$1,$2");
            }
    
        return value;
    } 
    

    我不知道这个解决方案是否对性能有影响,我也很喜欢正则表达式,所以这段代码可能会被缩短。

    【讨论】:

      【解决方案3】:

      试试这个,这些Locale 格式是你需要的格式。

          List<Locale> locales = Arrays.asList(new Locale("it", "CH"), new Locale("fr", "CH"), new Locale("de", "CH"));
          for (Locale locale : locales) {
              DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
              DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
              dfs.setCurrencySymbol("");
              df.setDecimalFormatSymbols(dfs);
              System.out.println(String.format("%5s %15s %15s", locale, format(df.format(1000)), format(df.format(1_000_000))));
          }
      

      实用方法

      private static String format(String str) {
          int index = str.lastIndexOf('\'');
          if (index > 0) {
              return new StringBuilder(str).replace(index, index + 1, ",").toString();
          }
          return str;
      }
      

      输出

      it_CH        1,000.00    1'000,000.00
      fr_CH        1,000.00    1'000,000.00
      de_CH        1,000.00    1'000,000.00
      

      设置df.setMaximumFractionDigits(0); 删除分数

      输出

      it_CH           1,000       1'000,000
      fr_CH           1,000       1'000,000
      de_CH           1,000       1'000,000
      

      【讨论】:

        【解决方案4】:

        也许可以尝试使用这个,“#”在空格或逗号之前加上你想要的单位。

        String num = "1000500000.574";
            String newnew = new   DecimalFormat("#,###.##").format(Double.parseDouble(number));
        

        【讨论】:

        • OP特别说DecimalFormat不起作用。
        • 感谢@TiagoFutre,但在我的具体情况下它的格式不起作用
        猜你喜欢
        • 2010-09-08
        • 2011-09-19
        • 2014-04-27
        • 1970-01-01
        • 1970-01-01
        • 2017-11-16
        • 2015-07-22
        相关资源
        最近更新 更多