X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.utils%2Fsrc%2Forg%2Fsimantics%2Futils%2Fformat%2FFormattingUtils.java;fp=bundles%2Forg.simantics.utils%2Fsrc%2Forg%2Fsimantics%2Futils%2Fformat%2FFormattingUtils.java;h=747da6b511c8af0ed17f89a158eeb62bab153e15;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtils.java new file mode 100644 index 000000000..747da6b51 --- /dev/null +++ b/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtils.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.utils.format; + +import java.math.BigDecimal; +import java.math.MathContext; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.FieldPosition; +import java.text.Format; +import java.text.NumberFormat; +import java.text.ParsePosition; +import java.util.Locale; + +/** + * @author Antti Villberg + */ +public class FormattingUtils { + + public static final FormattingUtil US = new FormattingUtil(5, 5, Locale.US); + public static final FormattingUtil DEFAULT = US; + + public static String engineeringFormat(Object value) { + return DEFAULT.engineeringFormat(value); + } + + public static Format significantDigitFormat(int withSignificantDigits) { + return significantDigitFormat(withSignificantDigits, false); + } + + public static Format significantDigitFormat(int withSignificantDigits, boolean stripTrailingZeros) { + if (withSignificantDigits < 1) + throw new IllegalArgumentException("withSignificantDigits must be > 0, got " + withSignificantDigits); + StringBuilder sb = new StringBuilder(withSignificantDigits + 3); + sb.append("0."); + for (int i = 0; i < withSignificantDigits-1; i++) + sb.append("#"); + sb.append("E0"); + NumberFormat low = new DecimalFormat(sb.toString()); + low.setGroupingUsed(false); + NumberFormat hi = new SignificantDigitFormat(withSignificantDigits, stripTrailingZeros); + Format format = new SwitchFormat(0.1, 1, low, hi, hi); + return format; + } + + private static class SignificantDigitFormat extends NumberFormat { + + private static final long serialVersionUID = 1079268918077141671L; + + private MathContext mc; + private boolean stripTrailingZeros; + private boolean fixDecimalSeparator; + private char decimalSeparator; + + public SignificantDigitFormat(int digits, boolean stripTrailingZeros) { + this.mc = new MathContext(digits); + this.stripTrailingZeros = stripTrailingZeros; + DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(); + decimalSeparator = symbols.getDecimalSeparator(); + // BigDecimal always formats doubles with '.' as decimal separator. + if (decimalSeparator != '.') + fixDecimalSeparator = true; + else + fixDecimalSeparator = false; + } + + @Override + public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) { + BigDecimal big = new BigDecimal(number, mc); + if (stripTrailingZeros) + big = big.stripTrailingZeros(); + String str = big.toString(); + if (fixDecimalSeparator) + str = str.replace('.', decimalSeparator); + return toAppendTo.append(str); + } + + @Override + public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) { + return toAppendTo.append(number); + } + + @Override + public Number parse(String source, ParsePosition parsePosition) { + return 0; + } + + } + +}