简体   繁体   English

如何在java中格式化int数字?

[英]How can I format int numbers in java?

I am trying to apply this format to a given int number #´###,### I tried with DecimalFormat class but it only allows to have one grouping separator symbol when I need to have two the accute accent for millions and commas for thousands. 我正在尝试将这种格式应用于给定的int数#´###,###我尝试使用DecimalFormat class但它只允许有一个分组分隔符符号,当我需要两个accute accent为数百万的逗号accute accent和逗号数千人。

So at the end I can format values like 1,000 or millions in this way 1´000,000 所以最后我可以用这种方式1´000,000 1,000或者数百万的值1´000,000

I always prefer to use String.format, but I am not sure if there is a Locale that would format numbers like that either. 我总是喜欢使用String.format,但我不确定是否有一个Locale可以格式化这样的数字。 Here is some code that will do the job though. 这里有一些代码可以完成这项工作。

// 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 += ",";
}

Try this, these Locale formats in your required format. 试试这些,这些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))));
    }

util method util方法

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

output 产量

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

set df.setMaximumFractionDigits(0); set df.setMaximumFractionDigits(0); to remove the fractions 去除分数

output 产量

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

I found useful the link @Roshan provide in comments, this solution is using regex expression and replaceFirst method 我发现有用的链接@Roshan在评论中提供,这个解决方案是使用正则表达式和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;
} 

I don't know if this solution has a performance impact, also I am rockie with regex, so this code might be shorted. 我不知道这个解决方案是否会对性能产生影响,而且我对正则表达式很不满意,所以这段代码可能会缩短。

Maybe try using this, the "#" in place with the units you want before the space or comma. 也许尝试使用这个,在空格或逗号之前使用你想要的单位“#”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM