|
|
@@ -0,0 +1,138 @@
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira
|
|
|
+ */
|
|
|
+package net.ranides.assira.text;
|
|
|
+
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author ranides
|
|
|
+ */
|
|
|
+public final class FormatNumber {
|
|
|
+
|
|
|
+ private FormatNumber() {
|
|
|
+ /* utility class */
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class UnitPrefix {
|
|
|
+
|
|
|
+ private final double v;
|
|
|
+ private final String u;
|
|
|
+
|
|
|
+ UnitPrefix(double v, String u) {
|
|
|
+ this.v = v;
|
|
|
+ this.u = u;
|
|
|
+ }
|
|
|
+
|
|
|
+ public double value() {
|
|
|
+ return v;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String text() {
|
|
|
+ return u;
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuilder append(StringBuilder target, int padding, int precision, double value) {
|
|
|
+ return target.append(
|
|
|
+ String.format(Locale.ENGLISH,
|
|
|
+ String.format("%%%d.%df%s", padding, precision, text()), value/value())
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final UnitPrefix[] METRIC_PREFIX = new UnitPrefix[]{
|
|
|
+ new UnitPrefix(Math.pow(1000, 8),"Y"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 7),"Z"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 6),"E"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 5),"P"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 4),"T"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 3),"G"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 2),"M"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 1),"k"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 0),""),
|
|
|
+ new UnitPrefix(Math.pow(1000, -1),"m"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -2),"μ"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -3),"n"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -4),"p"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -5),"f"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -6),"a"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -7),"z"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -8),"y")
|
|
|
+ };
|
|
|
+
|
|
|
+ public static final UnitPrefix[] METRIC_EXP = new UnitPrefix[]{
|
|
|
+ new UnitPrefix(Math.pow(1000, 8),"E24"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 7),"E21"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 6),"E18"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 5),"E15"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 4),"E12"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 3),"E9"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 2),"E6"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 1),"E3"),
|
|
|
+ new UnitPrefix(Math.pow(1000, 0),"E0"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -1),"E-3"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -2),"E-6"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -3),"E-9"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -4),"E-12"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -5),"E-15"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -6),"E-18"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -7),"E-21"),
|
|
|
+ new UnitPrefix(Math.pow(1000, -8),"E-24")
|
|
|
+ };
|
|
|
+
|
|
|
+ public static final UnitPrefix[] BYTE_PREFIX = new UnitPrefix[]{
|
|
|
+ new UnitPrefix(Math.pow(1024, 8),"Y"),
|
|
|
+ new UnitPrefix(Math.pow(1024, 7),"Z"),
|
|
|
+ new UnitPrefix(Math.pow(1024, 6),"E"),
|
|
|
+ new UnitPrefix(Math.pow(1024, 5),"P"),
|
|
|
+ new UnitPrefix(Math.pow(1024, 4),"T"),
|
|
|
+ new UnitPrefix(Math.pow(1024, 3),"G"),
|
|
|
+ new UnitPrefix(Math.pow(1024, 2),"M"),
|
|
|
+ new UnitPrefix(Math.pow(1024, 1),"K"),
|
|
|
+ new UnitPrefix(Math.pow(1024, 0),""),
|
|
|
+ };
|
|
|
+
|
|
|
+ public static String asEngineer(int padding, int precision, UnitPrefix[] units, double value) {
|
|
|
+ return asEngineer(new StringBuilder(), padding, precision, units, value).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static StringBuilder asEngineer(StringBuilder target, int padding, int precision, UnitPrefix[] units, double value) {
|
|
|
+ if(value < 0) {
|
|
|
+ target.append('-');
|
|
|
+ return asEngineer(target, padding, precision, units, -value);
|
|
|
+ }
|
|
|
+ UnitPrefix c = units[0];
|
|
|
+ for (UnitPrefix u : units) {
|
|
|
+ c = u;
|
|
|
+ if (value > c.value()) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return c.append(target, padding, precision, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String group(int value) {
|
|
|
+ return group(Integer.toString(value, 10), "'");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String group(long value) {
|
|
|
+ return group(Long.toString(value, 10), "'");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String group(String text, String separator) {
|
|
|
+ StringBuilder ret = new StringBuilder(text.length() + (text.length()/3 + 1) * separator.length());
|
|
|
+ for(int i=0, n=text.length()-1; i<=n; i++) {
|
|
|
+ ret.append(text.charAt(i));
|
|
|
+ if( n>i && ((n-i)%3 == 0) ) {
|
|
|
+ ret.append(separator);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ret.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|