string - How to convert an integer according to international number system in java (Number Formatting) -
i want convert given number according international number system. // example:
345678 -> 345,678 12 -> 12 123 -> 123 4590 -> 4,590
here code snippet , ideone link
public static void main (string[] args) throws java.lang.exception { int number = 345678; string s = integer.tostring(number); s = putcommas(s); } private string putcommas(string s) { // how put commas in string after every 3rd character right ? }
here approach not comfortable.
- start traversing string right
- if (length - currentindex -1) % 3 == 0 insert comma in string
any suggestions ?
you can use numberformat
locale#us
:
numberformat.getnumberinstance(locale.us).format(number);
or decimalformat
:
decimalformat formatter = new decimalformat("#,###"); string newnumber = formatter.format(number);
Comments
Post a Comment