user291701
user291701

Reputation: 39731

Put commas in a number string?

Given a number, I'd like to transform it into a string, but insert commas in the thousands place etc, like:

int number = 123456;
String formatted = String.valueOf(number);
println(formatted); // but print "123,456"?

does GWT offer a way of doing this, or should we write our own method?

Thanks

Upvotes: 1

Views: 779

Answers (2)

george_h
george_h

Reputation: 1592

The first one is to format a number with decimal points and include a comma. The other is with out decimal points. I'm giving this out because it wasn't so easy for me to figure it out the first time when I was starting out.

private NumberFormat decFormat = NumberFormat.getFormat("#,##0.00;(#,##0.00)");
private NumberFormat intFormat = NumberFormat.getFormat("#,##0;(#,##0)");

Upvotes: 3

Abhijith Nagaraja
Abhijith Nagaraja

Reputation: 3380

Use NumberFormat provided by GWT.

Upvotes: 1

Related Questions