diff options
| author | ilotterytea <iltsu@alright.party> | 2024-10-25 21:42:30 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-10-25 21:42:30 +0500 |
| commit | d9ccdc541f76bd93585b50803e4c018228672043 (patch) | |
| tree | eb5d99e958d9ed8f4e34b5370335283f3e9177f7 /core/src/kz/ilotterytea/maxon/utils | |
| parent | e97010bd0bbe83b4a567d921d60cc023fa7d2d8d (diff) | |
upd: number formatter
Diffstat (limited to 'core/src/kz/ilotterytea/maxon/utils')
| -rw-r--r-- | core/src/kz/ilotterytea/maxon/utils/formatters/NumberFormatter.java | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/core/src/kz/ilotterytea/maxon/utils/formatters/NumberFormatter.java b/core/src/kz/ilotterytea/maxon/utils/formatters/NumberFormatter.java index 812b79c..f6c0fa4 100644 --- a/core/src/kz/ilotterytea/maxon/utils/formatters/NumberFormatter.java +++ b/core/src/kz/ilotterytea/maxon/utils/formatters/NumberFormatter.java @@ -1,33 +1,56 @@ package kz.ilotterytea.maxon.utils.formatters; +import kz.ilotterytea.maxon.MaxonConstants; + import java.util.Map; import java.util.NavigableMap; import java.util.TreeMap; public class NumberFormatter { - private static final NavigableMap<Long, String> suffixes = new TreeMap<>(); + private static final NavigableMap<Double, String> suffixes = new TreeMap<>(); static { - suffixes.put(1_000L, "k"); - suffixes.put(1_000_000L, "M"); - suffixes.put(1_000_000_000L, "G"); - suffixes.put(1_000_000_000_000L, "T"); - suffixes.put(1_000_000_000_000_000L, "P"); - suffixes.put(1_000_000_000_000_000_000L, "E"); + suffixes.put(1_000.0, "k"); + suffixes.put(1_000_000.0, "M"); + suffixes.put(1_000_000_000.0, "B"); + suffixes.put(1_000_000_000_000.0, "T"); + suffixes.put(1_000_000_000_000_000.0, "Qd"); + suffixes.put(1_000_000_000_000_000_000.0, "Qi"); + suffixes.put(1_000_000_000_000_000_000_000.0, "Sx"); + suffixes.put(1_000_000_000_000_000_000_000_000.0, "Sp"); + suffixes.put(1_000_000_000_000_000_000_000_000_000.0, "O"); + suffixes.put(1_000_000_000_000_000_000_000_000_000_000.0, "N"); + suffixes.put(1_000_000_000_000_000_000_000_000_000_000_000.0, "D"); + suffixes.put(1_000_000_000_000_000_000_000_000_000_000_000_000.0, "Xz"); + } + + public static String format(double value) { + return format(value, true); } - public static String format(long value) { + public static String format(double value, boolean decimal) { //Long.MIN_VALUE == -Long.MIN_VALUE so we need an adjustment here - if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1); - if (value < 0) return "-" + format(-value); - if (value < 1000) return Long.toString(value); //deal with easy case + if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1, decimal); + if (value < 0) return "-" + format(-value, decimal); + if (value < 100_000.0) { + if (!decimal || value == Math.floor(value)) return MaxonConstants.DECIMAL_FORMAT2.format(value); + else return MaxonConstants.DECIMAL_FORMAT.format(value); + } - Map.Entry<Long, String> e = suffixes.floorEntry(value); - Long divideBy = e.getKey(); + Map.Entry<Double, String> e = suffixes.floorEntry(value); + Double divideBy = e.getKey(); String suffix = e.getValue(); - long truncated = value / (divideBy / 10); //the number part of the output times 10 + double truncated = value / (divideBy / 10.0); //the number part of the output times 10 boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10); - return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix; + return formatWithSuffix(truncated / 10d, suffix); + } + + private static String formatWithSuffix(double value, String suffix) { + if (value == Math.floor(value)) { + return String.format("%.0f%s", value, suffix); + } else { + return String.format("%.1f%s", value, suffix); + } } public static String pad(long value) { |
