When expressing the amount, sometimes you want to put a comma (,) in every 3 digits.
10000 -> 10,000
12345 -> 12,345
How to use DecimalFormat
You can define the string format using DecimalFormat.
The string format is passed as an argument when creating a DecimalFormat
object.
If you want to put a comma every 3 digits, set it in the format "###,###"
as shown below.
DecimalFormat decFormat = new DecimalFormat("###,###");
String str = decFormat.format(12300000);
System.out.println(str);
str = decFormat.format(505000);
System.out.println(str);
Output:
12,300,000
505,000
decimal output
If you want to print decimals, just type in the format like ".##"
.
"###,###.##"
means to put a comma in every 3 digits and to output only 2 decimal places.
NumberFormat numberFormat = NumberFormat.getInstance();
String str = numberFormat.format(123000);
System.out.println(str);
str = numberFormat.format(123000.7891);
System.out.println(str);
Output:
123,000
123,000.79
How to use NumberFormat
You can use NumberFormat to put commas in numbers.
NumberFormat formats the string in the notation used by the set Locale.
If you do not pass the Locale argument to getInstance()
when creating a NumberFormat object, Locale.US
is set as the default value, and commas are placed every 3 digits according to the US number notation.
NumberFormat numberFormat = NumberFormat.getInstance();
String str = numberFormat.format(123000);
System.out.println(str);
str = numberFormat.format(123000.7891);
System.out.println(str);
Output:
123,000
123,000.789
If you want to output in the number notation of another country, you can pass Locale as an argument when creating NumberFormat.
NumberFormat numberFormat = NumberFormat.getInstance(Locale.ITALY);
NumberFormat numberFormat2 = NumberFormat.getInstance(Locale.CHINA);
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)