How to Format Commas in a Number in Java

Java requires programmers to set up a pattern when formatting numbers with special characters such as the comma. You use the pattern format to display numbers greater than 999, so users can read the numbers more conveniently. The Java "NumberFormat" class formats the numbers in your desktop applications.

Instructions

    • 1

      Right-click the Java source code file and select "Open With." Click the Java compiler in your list of compatible programs.

    • 2

      Create the "NumberFormat" variable with the pattern string. The following code adds a comma to your numbers that exceed 999:

      DecimalFormat formating = new DecimalFormat("#,###");

      The hash tags indicate that the input is any number between zero and nine.

    • 3

      Apply the number format and display the result to your users. The following displays "9,000" to the user:

      int mynum = 9000;

      String result= formating.format(mynum);

      System.out.println(result);

Related Searches:

References

Comments

Related Ads

Featured