diff options
author | xander <xander@isxander.dev> | 2022-09-01 11:58:49 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-01 11:58:49 +0100 |
commit | 4d977cc9764ecf0073650f126700f6ff638fa06b (patch) | |
tree | 883e68bbd80874c048b3e34db59bf0aa926b489b /src/main/java/dev/isxander/yacl/impl/utils | |
parent | e63a3c989e3a899bdc81558dd2e4c5cc2c659bde (diff) | |
download | YetAnotherConfigLib-4d977cc9764ecf0073650f126700f6ff638fa06b.tar.gz YetAnotherConfigLib-4d977cc9764ecf0073650f126700f6ff638fa06b.tar.bz2 YetAnotherConfigLib-4d977cc9764ecf0073650f126700f6ff638fa06b.zip |
javadoc!
added LongSliderController
renamed Control -> Controller
add minecraft simple option binding constructor
Diffstat (limited to 'src/main/java/dev/isxander/yacl/impl/utils')
-rw-r--r-- | src/main/java/dev/isxander/yacl/impl/utils/NumberFormatHelper.java | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl/impl/utils/NumberFormatHelper.java b/src/main/java/dev/isxander/yacl/impl/utils/NumberFormatHelper.java new file mode 100644 index 0000000..bbf44a1 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/utils/NumberFormatHelper.java @@ -0,0 +1,38 @@ +package dev.isxander.yacl.impl.utils; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; + +@ApiStatus.Internal +public class NumberFormatHelper { + private static final NavigableMap<Long, String> suffixes = new TreeMap<>(); + static { + suffixes.put(1_000L, "K"); + suffixes.put(1_000_000L, "M"); + suffixes.put(1_000_000_000L, "B"); + 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"); + } + + /** + * @author <a href="https://stackoverflow.com/a/30661479">assylias</a> + */ + public static String formatSmaller(long value) { + // Long.MIN_VALUE == -Long.MIN_VALUE, so we need an adjustment here + if (value == Long.MIN_VALUE) return formatSmaller(Long.MIN_VALUE + 1); + if (value < 0) return "-" + formatSmaller(-value); + if (value < 1000) return Long.toString(value); //deal with easy case + + Map.Entry<Long, String> e = suffixes.floorEntry(value); + Long divideBy = e.getKey(); + String suffix = e.getValue(); + + long truncated = value / (divideBy / 10); //the number part of the output times 10 + boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10); + return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix; + } +} |