开发者

Converting Integer to String with comma for thousands

I want to convert an Integer 35634646 to have the thousand "," so it should be 35,634,646.开发者_运维技巧

What would be the quickest way to doing that?


System.out.println(NumberFormat.getNumberInstance(Locale.US).format(35634646));
Output: 35,634,646


You ask for quickest, but perhaps you mean "best" or "correct" or "typical"?

You also ask for commas to indicate thousands, but perhaps you mean "in normal human readable form according to the local custom of your user"?

You do it as so:

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

Americans will get "35,634,646"

Germans will get "35.634.646"

Swiss Germans will get "35'634'646"


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


Integers:

int value = 100000; 
String.format("%,d", value); // outputs 100,000

Doubles:

double value = 21403.3144d;
String.format("%,.2f", value); // outputs 21,403.31

String.format is pretty powerful.

- Edited per psuzzi feedback.


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

Output: 35,634,646


The other answers are correct, however double-check your locale before using "%,d":

Locale.setDefault(Locale.US);
int bigNumber = 35634646;
String formattedNumber = String.format("%,d", bigNumber);
System.out.println(formattedNumber);

Locale.setDefault(new Locale("pl", "PL"));
formattedNumber = String.format("%,d", bigNumber);
System.out.println(formattedNumber);

Result:

35,634,646
35 634 646


use Extension

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)

result

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


This solution worked for me:

NumberFormat.getNumberInstance(Locale.US).format(Integer.valueOf("String Your Number"));


Use the %d format specifier with a comma: %,d

This is by far the easiest way.


here's a solution for those of you who can't access "numberformat" nor "String.format" (using a limited version of java inside a framework). Hope it's useful.

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


If the same has to be done in the JSP , use:

<fmt:formatNumber pattern="#,##0" value="${yourlist.yourintvalue}" var="formattedVariable" />
<c:out value="${formattedVariable}"></c:out>

ofcourse for multiple values use :

<c:forEach items="${yourlist}" var="yourlist">

    <fmt:formatNumber pattern="#,##0" value="${yourlist.yourintvalue}" var="formattedVariable" />
    <c:out value="${formattedVariable}"></c:out>
</c:forEach>


This is a way that also able you to replace default separator with any characters:

val myNumber = NumberFormat.getNumberInstance(Locale.US)
   .format(123456789)
   .replace(",", "،")


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


can't you use a

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

The comma in the printf should add the commas into the %d inter.

Not positive about it, but works for me.


First you need to include the JSTL tags :-

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

at the start of the page

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜