Skip to content

Commit

Permalink
Compact Number Formatting (java-12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimatay committed Sep 8, 2024
1 parent 3ba20fe commit 8258a84
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ This repository contains Java examples that are designed to track and document t
* [Java 16](java-16/) (March, 2021)
* [JEP 395](java-16/src/main/java/com/ibrahimatay/JEP395Records.java): Records

* [Java 12](java-12/) (March, 2019)
* API Improvements
* [Compact Number Formatting](java-12/src/main/java/com/ibrahimatay/CompactNumberFormatting.java)

* [Java 11](java-11/) (September, 2018)
* [JEP 181](java-11/src/main/java/com/ibrahimatay/JEP181NestBasedAccessControl.java): Nest-Based Access Control
* [JEP 318](java-11/src/main/java/com/ibrahimatay/JEP318MemoryPolluter.java): Epsilon: A No-Op Garbage Collector
Expand Down
50 changes: 50 additions & 0 deletions java-12/src/main/java/com/ibrahimatay/CompactNumberFormatting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.ibrahimatay;

import java.text.CompactNumberFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.*;

public class CompactNumberFormatting {

// Compact Number Formatting support
// https://bugs.openjdk.org/browse/JDK-8188147

// Compact Number Formatting Comes to JDK 12
// https://marxsoftware.blogspot.com/2019/01/jdk12-custom-number-formatting.html

public static void main(String[] args) throws ParseException {
NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);

System.out.println(fmt.parse("100"));
System.out.println(fmt.parse("1 thousand"));
System.out.println(fmt.parse("10 thousand"));
System.out.println(fmt.parse("100 thousand"));

/*
Result:
100
1000
10000
100000
*/
NumberFormat minimumFractionDigits = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
minimumFractionDigits.setMinimumFractionDigits(3);

System.out.println(minimumFractionDigits.format(10000));
System.out.println(minimumFractionDigits.format(10012));
System.out.println(minimumFractionDigits.format(100201));
System.out.println(minimumFractionDigits.format(1111111));

/*
Result:
10.000K
10.012K
100.201K
1.111M
*/
}
}

7 changes: 0 additions & 7 deletions java-12/src/main/java/com/ibrahimatay/Main.java

This file was deleted.

0 comments on commit 8258a84

Please sign in to comment.