java - Change the format of a number to readable in currency format? -
in application, want able to:
make number 300000 appear 300,000 usd ? don't want "usd" ,
i want able choose own currency too.
to described, try counting every 3 digits (chars) end of string number , add ','
. then, @ end, add " usd" (space makes nicer).
example code:
int toformat = 1563992; string output = "" + toformat; int counter = 0; (int index = output.length() - 1; index > 0; index--){ counter++; if (counter % 3 == 0){ counter = 0; output = output.substring(0,index) + "," + output.substring(index); } } output += " usd"; // or whatever want system.out.println(output);
outputs: 1,563,992 usd
Comments
Post a Comment