1

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

user291701
  • 38,411
  • 72
  • 187
  • 285
  • See http://stackoverflow.com/questions/1449805/how-to-format-a-number-from-1123456789-to-1-123-456-789-in-c/1449859#1449859 for a C version which should be easy enough to port to Java. – paxdiablo Jan 29 '13 at 02:46
  • See http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/i18n/client/NumberFormat.html – favoretti Jan 29 '13 at 02:47
  • Refer http://stackoverflow.com/questions/4713166/decimal-separator-in-numberformat – Syam Kumar S Jan 29 '13 at 06:25

2 Answers2

3

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)");
george_h
  • 1,562
  • 2
  • 19
  • 37
1

Use NumberFormat provided by GWT.

Abhijith Nagaraja
  • 3,370
  • 6
  • 27
  • 55