From 8258a847361661a7af4e22300a4757307397fd71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0brahim=20ATAY?= Date: Sun, 8 Sep 2024 13:12:32 +0000 Subject: [PATCH] Compact Number Formatting (java-12) --- README.md | 4 ++ .../ibrahimatay/CompactNumberFormatting.java | 50 +++++++++++++++++++ .../src/main/java/com/ibrahimatay/Main.java | 7 --- 3 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 java-12/src/main/java/com/ibrahimatay/CompactNumberFormatting.java delete mode 100644 java-12/src/main/java/com/ibrahimatay/Main.java diff --git a/README.md b/README.md index c1b1e92..5d3ba79 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/java-12/src/main/java/com/ibrahimatay/CompactNumberFormatting.java b/java-12/src/main/java/com/ibrahimatay/CompactNumberFormatting.java new file mode 100644 index 0000000..597cda6 --- /dev/null +++ b/java-12/src/main/java/com/ibrahimatay/CompactNumberFormatting.java @@ -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 + */ + } +} + diff --git a/java-12/src/main/java/com/ibrahimatay/Main.java b/java-12/src/main/java/com/ibrahimatay/Main.java deleted file mode 100644 index 4ca8a0e..0000000 --- a/java-12/src/main/java/com/ibrahimatay/Main.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.ibrahimatay; - -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file