import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
+
/**
*
* @author James Ahlborn
private static final String POS_INF_STR = "1.#INF";
private static final String NEG_INf_STR = "-1.#INF";
- private static final ThreadLocal<NumberFormatter> INSTANCE =
- new ThreadLocal<NumberFormatter>() {
- @Override
- protected NumberFormatter initialValue() {
- return new NumberFormatter();
- }
- };
-
- private final TypeFormatter _fltFmt = new TypeFormatter(FLT_SIG_DIGITS);
- private final TypeFormatter _dblFmt = new TypeFormatter(DBL_SIG_DIGITS);
- private final TypeFormatter _decFmt = new TypeFormatter(DEC_SIG_DIGITS);
-
- private NumberFormatter() {}
-
- public static String format(float f) {
- return INSTANCE.get().formatImpl(f);
- }
-
- public static String format(double d) {
- return INSTANCE.get().formatImpl(d);
- }
+ private final TypeFormatter _fltFmt;
+ private final TypeFormatter _dblFmt;
+ private final TypeFormatter _decFmt;
- public static String format(BigDecimal bd) {
- return INSTANCE.get().formatImpl(bd);
+ public NumberFormatter(DecimalFormatSymbols syms) {
+ _fltFmt = new TypeFormatter(FLT_SIG_DIGITS, syms);
+ _dblFmt = new TypeFormatter(DBL_SIG_DIGITS, syms);
+ _decFmt = new TypeFormatter(DEC_SIG_DIGITS, syms);
}
- private String formatImpl(float f) {
+ public String format(float f) {
if(Float.isNaN(f)) {
return NAN_STR;
return _fltFmt.format(new BigDecimal(f, FLT_MATH_CONTEXT));
}
- private String formatImpl(double d) {
+ public String format(double d) {
if(Double.isNaN(d)) {
return NAN_STR;
return _dblFmt.format(new BigDecimal(d, DBL_MATH_CONTEXT));
}
- private String formatImpl(BigDecimal bd) {
+ public String format(BigDecimal bd) {
return _decFmt.format(bd.round(DEC_MATH_CONTEXT));
}
- private static ScientificFormat createScientificFormat(int prec) {
- DecimalFormat df = new DecimalFormat("0.#E00");
+ private static ScientificFormat createScientificFormat(
+ int prec, DecimalFormatSymbols syms) {
+ DecimalFormat df = new DecimalFormat("0.#E00", syms);
df.setMaximumIntegerDigits(1);
df.setMaximumFractionDigits(prec);
df.setRoundingMode(ROUND_MODE);
private static final class TypeFormatter
{
- private final DecimalFormat _df = new DecimalFormat("0.#");
+ private final DecimalFormat _df;
private final ScientificFormat _dfS;
private final int _prec;
- private TypeFormatter(int prec) {
+ private TypeFormatter(int prec, DecimalFormatSymbols syms) {
_prec = prec;
+ _df = new DecimalFormat("0.#", syms);
_df.setMaximumIntegerDigits(prec);
_df.setMaximumFractionDigits(prec);
_df.setRoundingMode(ROUND_MODE);
- _dfS = createScientificFormat(prec);
+ _dfS = createScientificFormat(prec, syms);
}
public String format(BigDecimal bd) {
import java.math.BigDecimal;
+import com.healthmarketscience.jackcess.expr.NumericConfig;
import static junit.framework.TestCase.assertEquals;
import org.junit.Test;
*/
public class NumberFormatterTest
{
+ private NumberFormatter _numFmt = new NumberFormatter(
+ NumericConfig.US_NUMERIC_CONFIG.getDecimalFormatSymbols());
@Test
public void testDoubleFormat() throws Exception
{
- assertEquals("894984737284944", NumberFormatter.format(894984737284944d));
- assertEquals("-894984737284944", NumberFormatter.format(-894984737284944d));
- assertEquals("8949.84737284944", NumberFormatter.format(8949.84737284944d));
- assertEquals("8949847372844", NumberFormatter.format(8949847372844d));
- assertEquals("8949.847384944", NumberFormatter.format(8949.847384944d));
- assertEquals("8.94985647372849E+16", NumberFormatter.format(89498564737284944d));
- assertEquals("-8.94985647372849E+16", NumberFormatter.format(-89498564737284944d));
- assertEquals("895649.847372849", NumberFormatter.format(895649.84737284944d));
- assertEquals("300", NumberFormatter.format(300d));
- assertEquals("-300", NumberFormatter.format(-300d));
- assertEquals("0.3", NumberFormatter.format(0.3d));
- assertEquals("0.1", NumberFormatter.format(0.1d));
- assertEquals("2.3423421E-12", NumberFormatter.format(0.0000000000023423421d));
- assertEquals("2.3423421E-11", NumberFormatter.format(0.000000000023423421d));
- assertEquals("2.3423421E-10", NumberFormatter.format(0.00000000023423421d));
- assertEquals("-2.3423421E-10", NumberFormatter.format(-0.00000000023423421d));
- assertEquals("2.34234214E-12", NumberFormatter.format(0.00000000000234234214d));
- assertEquals("2.342342156E-12", NumberFormatter.format(0.000000000002342342156d));
- assertEquals("0.000000023423421", NumberFormatter.format(0.000000023423421d));
- assertEquals("2.342342133E-07", NumberFormatter.format(0.0000002342342133d));
- assertEquals("1.#INF", NumberFormatter.format(Double.POSITIVE_INFINITY));
- assertEquals("-1.#INF", NumberFormatter.format(Double.NEGATIVE_INFINITY));
- assertEquals("1.#QNAN", NumberFormatter.format(Double.NaN));
+ assertEquals("894984737284944", _numFmt.format(894984737284944d));
+ assertEquals("-894984737284944", _numFmt.format(-894984737284944d));
+ assertEquals("8949.84737284944", _numFmt.format(8949.84737284944d));
+ assertEquals("8949847372844", _numFmt.format(8949847372844d));
+ assertEquals("8949.847384944", _numFmt.format(8949.847384944d));
+ assertEquals("8.94985647372849E+16", _numFmt.format(89498564737284944d));
+ assertEquals("-8.94985647372849E+16", _numFmt.format(-89498564737284944d));
+ assertEquals("895649.847372849", _numFmt.format(895649.84737284944d));
+ assertEquals("300", _numFmt.format(300d));
+ assertEquals("-300", _numFmt.format(-300d));
+ assertEquals("0.3", _numFmt.format(0.3d));
+ assertEquals("0.1", _numFmt.format(0.1d));
+ assertEquals("2.3423421E-12", _numFmt.format(0.0000000000023423421d));
+ assertEquals("2.3423421E-11", _numFmt.format(0.000000000023423421d));
+ assertEquals("2.3423421E-10", _numFmt.format(0.00000000023423421d));
+ assertEquals("-2.3423421E-10", _numFmt.format(-0.00000000023423421d));
+ assertEquals("2.34234214E-12", _numFmt.format(0.00000000000234234214d));
+ assertEquals("2.342342156E-12", _numFmt.format(0.000000000002342342156d));
+ assertEquals("0.000000023423421", _numFmt.format(0.000000023423421d));
+ assertEquals("2.342342133E-07", _numFmt.format(0.0000002342342133d));
+ assertEquals("1.#INF", _numFmt.format(Double.POSITIVE_INFINITY));
+ assertEquals("-1.#INF", _numFmt.format(Double.NEGATIVE_INFINITY));
+ assertEquals("1.#QNAN", _numFmt.format(Double.NaN));
}
@Test
public void testFloatFormat() throws Exception
{
- assertEquals("8949847", NumberFormatter.format(8949847f));
- assertEquals("-8949847", NumberFormatter.format(-8949847f));
- assertEquals("8949.847", NumberFormatter.format(8949.847f));
- assertEquals("894984", NumberFormatter.format(894984f));
- assertEquals("8949.84", NumberFormatter.format(8949.84f));
- assertEquals("8.949856E+16", NumberFormatter.format(89498564737284944f));
- assertEquals("-8.949856E+16", NumberFormatter.format(-89498564737284944f));
- assertEquals("895649.9", NumberFormatter.format(895649.84737284944f));
- assertEquals("300", NumberFormatter.format(300f));
- assertEquals("-300", NumberFormatter.format(-300f));
- assertEquals("0.3", NumberFormatter.format(0.3f));
- assertEquals("0.1", NumberFormatter.format(0.1f));
- assertEquals("2.342342E-12", NumberFormatter.format(0.0000000000023423421f));
- assertEquals("2.342342E-11", NumberFormatter.format(0.000000000023423421f));
- assertEquals("2.342342E-10", NumberFormatter.format(0.00000000023423421f));
- assertEquals("-2.342342E-10", NumberFormatter.format(-0.00000000023423421f));
- assertEquals("2.342342E-12", NumberFormatter.format(0.00000000000234234214f));
- assertEquals("2.342342E-12", NumberFormatter.format(0.000000000002342342156f));
- assertEquals("0.0000234", NumberFormatter.format(0.0000234f));
- assertEquals("2.342E-05", NumberFormatter.format(0.00002342f));
- assertEquals("1.#INF", NumberFormatter.format(Float.POSITIVE_INFINITY));
- assertEquals("-1.#INF", NumberFormatter.format(Float.NEGATIVE_INFINITY));
- assertEquals("1.#QNAN", NumberFormatter.format(Float.NaN));
+ assertEquals("8949847", _numFmt.format(8949847f));
+ assertEquals("-8949847", _numFmt.format(-8949847f));
+ assertEquals("8949.847", _numFmt.format(8949.847f));
+ assertEquals("894984", _numFmt.format(894984f));
+ assertEquals("8949.84", _numFmt.format(8949.84f));
+ assertEquals("8.949856E+16", _numFmt.format(89498564737284944f));
+ assertEquals("-8.949856E+16", _numFmt.format(-89498564737284944f));
+ assertEquals("895649.9", _numFmt.format(895649.84737284944f));
+ assertEquals("300", _numFmt.format(300f));
+ assertEquals("-300", _numFmt.format(-300f));
+ assertEquals("0.3", _numFmt.format(0.3f));
+ assertEquals("0.1", _numFmt.format(0.1f));
+ assertEquals("2.342342E-12", _numFmt.format(0.0000000000023423421f));
+ assertEquals("2.342342E-11", _numFmt.format(0.000000000023423421f));
+ assertEquals("2.342342E-10", _numFmt.format(0.00000000023423421f));
+ assertEquals("-2.342342E-10", _numFmt.format(-0.00000000023423421f));
+ assertEquals("2.342342E-12", _numFmt.format(0.00000000000234234214f));
+ assertEquals("2.342342E-12", _numFmt.format(0.000000000002342342156f));
+ assertEquals("0.0000234", _numFmt.format(0.0000234f));
+ assertEquals("2.342E-05", _numFmt.format(0.00002342f));
+ assertEquals("1.#INF", _numFmt.format(Float.POSITIVE_INFINITY));
+ assertEquals("-1.#INF", _numFmt.format(Float.NEGATIVE_INFINITY));
+ assertEquals("1.#QNAN", _numFmt.format(Float.NaN));
}
@Test
public void testDecimalFormat() throws Exception
{
- assertEquals("9874539485972.2342342234234", NumberFormatter.format(new BigDecimal("9874539485972.2342342234234")));
- assertEquals("9874539485972.234234223423468", NumberFormatter.format(new BigDecimal("9874539485972.2342342234234678")));
- assertEquals("-9874539485972.234234223423468", NumberFormatter.format(new BigDecimal("-9874539485972.2342342234234678")));
- assertEquals("9.874539485972234234223423468E+31", NumberFormatter.format(new BigDecimal("98745394859722342342234234678000")));
- assertEquals("9.874539485972234234223423468E+31", NumberFormatter.format(new BigDecimal("98745394859722342342234234678000")));
- assertEquals("-9.874539485972234234223423468E+31", NumberFormatter.format(new BigDecimal("-98745394859722342342234234678000")));
- assertEquals("300", NumberFormatter.format(new BigDecimal("300.0")));
- assertEquals("-300", NumberFormatter.format(new BigDecimal("-300.000")));
- assertEquals("0.3", NumberFormatter.format(new BigDecimal("0.3")));
- assertEquals("0.1", NumberFormatter.format(new BigDecimal("0.1000")));
- assertEquals("0.0000000000023423428930458", NumberFormatter.format(new BigDecimal("0.0000000000023423428930458")));
- assertEquals("2.3423428930458389038451E-12", NumberFormatter.format(new BigDecimal("0.0000000000023423428930458389038451")));
- assertEquals("2.342342893045838903845134766E-12", NumberFormatter.format(new BigDecimal("0.0000000000023423428930458389038451347656")));
+ assertEquals("9874539485972.2342342234234", _numFmt.format(new BigDecimal("9874539485972.2342342234234")));
+ assertEquals("9874539485972.234234223423468", _numFmt.format(new BigDecimal("9874539485972.2342342234234678")));
+ assertEquals("-9874539485972.234234223423468", _numFmt.format(new BigDecimal("-9874539485972.2342342234234678")));
+ assertEquals("9.874539485972234234223423468E+31", _numFmt.format(new BigDecimal("98745394859722342342234234678000")));
+ assertEquals("9.874539485972234234223423468E+31", _numFmt.format(new BigDecimal("98745394859722342342234234678000")));
+ assertEquals("-9.874539485972234234223423468E+31", _numFmt.format(new BigDecimal("-98745394859722342342234234678000")));
+ assertEquals("300", _numFmt.format(new BigDecimal("300.0")));
+ assertEquals("-300", _numFmt.format(new BigDecimal("-300.000")));
+ assertEquals("0.3", _numFmt.format(new BigDecimal("0.3")));
+ assertEquals("0.1", _numFmt.format(new BigDecimal("0.1000")));
+ assertEquals("0.0000000000023423428930458", _numFmt.format(new BigDecimal("0.0000000000023423428930458")));
+ assertEquals("2.3423428930458389038451E-12", _numFmt.format(new BigDecimal("0.0000000000023423428930458389038451")));
+ assertEquals("2.342342893045838903845134766E-12", _numFmt.format(new BigDecimal("0.0000000000023423428930458389038451347656")));
}
}