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

最快的方法是什么?


当前回答

这里有一个解决方案,你谁不能访问“数字格式”或“字符串。Format”(在框架中使用有限版本的Java)。希望对大家有用。

number= 123456789;
thousandsSeparator=",";
myNumberString=number.toString();   
numberLength=myNumberString.length;
howManySeparators=Math.floor((numberLength-1)/3)
formattedString=myNumberString.substring(0,numberLength-(howManySeparators*3))
while (howManySeparators>0)    {
formattedString=formattedString+thousandsSeparator+myNumberString.substring(numberLength-(howManySeparators*3),numberLength-((howManySeparators-1)*3));
howManySeparators=howManySeparators-1;    }

formattedString

其他回答

这里有一个解决方案,你谁不能访问“数字格式”或“字符串。Format”(在框架中使用有限版本的Java)。希望对大家有用。

number= 123456789;
thousandsSeparator=",";
myNumberString=number.toString();   
numberLength=myNumberString.length;
howManySeparators=Math.floor((numberLength-1)/3)
formattedString=myNumberString.substring(0,numberLength-(howManySeparators*3))
while (howManySeparators>0)    {
formattedString=formattedString+thousandsSeparator+myNumberString.substring(numberLength-(howManySeparators*3),numberLength-((howManySeparators-1)*3));
howManySeparators=howManySeparators-1;    }

formattedString

你不能用a吗

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

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

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

 int value = 35634646;
 DecimalFormat myFormatter = new DecimalFormat("#,###");
 String output = myFormatter.format(value);
 System.out.println(output);

输出:35634646

/**
@inpute 100000
@return 1,000,000
*/
    public static String addComma(long amount) {
        return NumberFormat.getNumberInstance(Locale.US).format(amount);
    }

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

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

在页面的开头