Jump to content

Number Formatting

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
dunnkers

dunnkers

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Hello,
I'm creating a method which formats big numbers. I want a decimal separator.
For example: How can i change 14000000 into 14,000,000?
I'm using NumberFormat and or DecimalFormat.
I currently have this code: (I know its tiny)

	String numberFormat(final int number) {

		DecimalFormat formatter = new DecimalFormat("#.##");

		return (formatter.format(number));

	}



#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
To avoid any hacks as a string, you can simply print it in a UK locale with right decimal separator (JDK 1.5+)
System.out.printf(Locale.UK, "%6.2f%n", 123456.78);
A more complete example using locales:
import java.text.NumberFormat;
import java.util.Locale;

class DecimalFormat1 {
    public static void main(String args[]) {
        NumberFormat nf2 =
        NumberFormat.getInstance(Locale.UK);
        System.out.println(nf2.format(1234.56));
    }
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
dunnkers

dunnkers

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Thanks alot Nullw0rm!
I just couldn't work it out myself..