Search in sources :

Example 1 with BigDecimalNumericFormatter

use of org.apache.tapestry5.internal.translator.BigDecimalNumericFormatter in project tapestry-5 by apache.

the class TranslatorSourceImplTest method bigdecimal_with_localized_symbols.

@Test
public void bigdecimal_with_localized_symbols() throws ParseException {
    DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);
    symbols.setGroupingSeparator('_');
    symbols.setMinusSign('*');
    symbols.setDecimalSeparator('#');
    BigDecimalNumericFormatter f = new BigDecimalNumericFormatter(symbols);
    BigDecimal big = new BigDecimal("-123456.797956563434");
    assertEquals(f.parse("*123_456#797956563434"), big);
    assertEquals(f.toClient(big), "*123456#797956563434");
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) BigDecimalNumericFormatter(org.apache.tapestry5.internal.translator.BigDecimalNumericFormatter) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 2 with BigDecimalNumericFormatter

use of org.apache.tapestry5.internal.translator.BigDecimalNumericFormatter in project tapestry-5 by apache.

the class NumericTranslatorSupportImpl method getOutputFormatter.

private NumericFormatter getOutputFormatter(Class type) {
    Locale locale = threadLocale.getLocale();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    if (type.equals(BigInteger.class))
        return new BigIntegerNumericFormatter(symbols);
    if (type.equals(BigDecimal.class))
        return new BigDecimalNumericFormatter(symbols);
    if (!isIntegerType(type)) {
        NumberFormat format = NumberFormat.getNumberInstance(locale);
        return new NumericFormatterImpl(format);
    }
    DecimalFormat df = new DecimalFormat(toString(symbols.getZeroDigit()), symbols);
    return new NumericFormatterImpl(df);
}
Also used : ThreadLocale(org.apache.tapestry5.ioc.services.ThreadLocale) Locale(java.util.Locale) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Example 3 with BigDecimalNumericFormatter

use of org.apache.tapestry5.internal.translator.BigDecimalNumericFormatter in project tapestry-5 by apache.

the class NumericTranslatorSupportImpl method getParseFormatter.

private NumericFormatter getParseFormatter(Class type) {
    Locale locale = threadLocale.getLocale();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    if (type.equals(BigInteger.class))
        return new BigIntegerNumericFormatter(symbols);
    if (type.equals(BigDecimal.class))
        return new BigDecimalNumericFormatter(symbols);
    if (isIntegerType(type)) {
        NumberFormat format = NumberFormat.getIntegerInstance(locale);
        return new NumericFormatterImpl(format);
    }
    DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(locale);
    if (type.equals(BigDecimal.class))
        df.setParseBigDecimal(true);
    return new NumericFormatterImpl(df);
}
Also used : ThreadLocale(org.apache.tapestry5.ioc.services.ThreadLocale) Locale(java.util.Locale) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) NumberFormat(java.text.NumberFormat)

Aggregations

DecimalFormatSymbols (java.text.DecimalFormatSymbols)3 DecimalFormat (java.text.DecimalFormat)2 NumberFormat (java.text.NumberFormat)2 Locale (java.util.Locale)2 ThreadLocale (org.apache.tapestry5.ioc.services.ThreadLocale)2 BigDecimal (java.math.BigDecimal)1 BigDecimalNumericFormatter (org.apache.tapestry5.internal.translator.BigDecimalNumericFormatter)1 Test (org.testng.annotations.Test)1