我想将一个整数35634646转换为1000个“”,所以它应该是35,634,646。

最快的方法是什么?


当前回答

你不能用a吗

System.out.printf("%n%,d",int name);

printf中的逗号应该将逗号添加到%d inter中。

不是很肯定,但对我来说还行。

其他回答

首先,你需要包括JSTL标签:-

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

在页面的开头

int bigNumber = 1234567;
String formattedNumber = String.format("%,d", bigNumber);

你不能用a吗

System.out.printf("%n%,d",int name);

printf中的逗号应该将逗号添加到%d inter中。

不是很肯定,但对我来说还行。

使用扩展

import java.text.NumberFormat

val Int.commaString: String
  get() = NumberFormat.getInstance().format(this)

val String.commaString: String
  get() = NumberFormat.getNumberInstance().format(this.toDouble())

val Long.commaString: String
  get() = NumberFormat.getInstance().format(this)

val Double.commaString: String
  get() = NumberFormat.getInstance().format(this)

结果

1234.commaString => 1,234
"1234.456".commaString => 1,234.456
1234567890123456789.commaString => 1,234,567,890,123,456,789
1234.456.commaString => 1,234.456

你要求最快,但也许你的意思是“最好”、“正确”或“典型”?

您还要求使用逗号来表示数千,但也许您的意思是“按照用户的本地习惯使用正常的人类可读格式”?

你可以这样做:

    int i = 35634646;
    String s = NumberFormat.getIntegerInstance().format(i);

美国人将得到" 35634,646 "

德国人将得到“35.634.646”

瑞士籍德国人将得到"35'634'646"