--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.format;\r
+\r
+import java.math.BigDecimal;\r
+import java.math.MathContext;\r
+import java.text.DecimalFormat;\r
+import java.text.DecimalFormatSymbols;\r
+import java.text.FieldPosition;\r
+import java.text.Format;\r
+import java.text.NumberFormat;\r
+import java.text.ParsePosition;\r
+import java.util.Locale;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class FormattingUtils {\r
+\r
+ public static final FormattingUtil US = new FormattingUtil(5, 5, Locale.US);\r
+ public static final FormattingUtil DEFAULT = US;\r
+\r
+ public static String engineeringFormat(Object value) {\r
+ return DEFAULT.engineeringFormat(value);\r
+ }\r
+\r
+ public static Format significantDigitFormat(int withSignificantDigits) {\r
+ return significantDigitFormat(withSignificantDigits, false);\r
+ }\r
+\r
+ public static Format significantDigitFormat(int withSignificantDigits, boolean stripTrailingZeros) {\r
+ if (withSignificantDigits < 1)\r
+ throw new IllegalArgumentException("withSignificantDigits must be > 0, got " + withSignificantDigits);\r
+ StringBuilder sb = new StringBuilder(withSignificantDigits + 3);\r
+ sb.append("0.");\r
+ for (int i = 0; i < withSignificantDigits-1; i++)\r
+ sb.append("#");\r
+ sb.append("E0");\r
+ NumberFormat low = new DecimalFormat(sb.toString());\r
+ low.setGroupingUsed(false);\r
+ NumberFormat hi = new SignificantDigitFormat(withSignificantDigits, stripTrailingZeros);\r
+ Format format = new SwitchFormat(0.1, 1, low, hi, hi);\r
+ return format;\r
+ }\r
+\r
+ private static class SignificantDigitFormat extends NumberFormat {\r
+\r
+ private static final long serialVersionUID = 1079268918077141671L;\r
+\r
+ private MathContext mc;\r
+ private boolean stripTrailingZeros;\r
+ private boolean fixDecimalSeparator;\r
+ private char decimalSeparator;\r
+\r
+ public SignificantDigitFormat(int digits, boolean stripTrailingZeros) {\r
+ this.mc = new MathContext(digits);\r
+ this.stripTrailingZeros = stripTrailingZeros;\r
+ DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();\r
+ decimalSeparator = symbols.getDecimalSeparator();\r
+ // BigDecimal always formats doubles with '.' as decimal separator.\r
+ if (decimalSeparator != '.')\r
+ fixDecimalSeparator = true;\r
+ else\r
+ fixDecimalSeparator = false;\r
+ }\r
+\r
+ @Override\r
+ public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {\r
+ BigDecimal big = new BigDecimal(number, mc);\r
+ if (stripTrailingZeros)\r
+ big = big.stripTrailingZeros();\r
+ String str = big.toString();\r
+ if (fixDecimalSeparator)\r
+ str = str.replace('.', decimalSeparator);\r
+ return toAppendTo.append(str);\r
+ }\r
+\r
+ @Override\r
+ public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {\r
+ return toAppendTo.append(number);\r
+ }\r
+\r
+ @Override\r
+ public Number parse(String source, ParsePosition parsePosition) {\r
+ return 0;\r
+ }\r
+\r
+ }\r
+\r
+}\r