private Formatter<Number> configureFormatterFrom(NumberFormat annotation) { String pattern = resolveEmbeddedValue(annotation.pattern()); if (StringUtils.hasLength(pattern)) { return new NumberStyleFormatter(pattern); } else { Style style = annotation.style(); if (style == Style.CURRENCY) { return new CurrencyStyleFormatter(); } else if (style == Style.PERCENT) { return new PercentStyleFormatter(); } else { return new NumberStyleFormatter(); } } }
@Test public void formatValue() { assertEquals("23%", formatter.print(new BigDecimal(".23"), Locale.US)); }
@Test(expected = ParseException.class) public void parsePercentValueNotLenientFailure() throws ParseException { formatter.parse("23.56%bogus", Locale.US); }
@Test public void printFormattedScopedAttributeResult() throws Exception { PercentStyleFormatter formatter = new PercentStyleFormatter(); tag.setExpression("bean.formattable"); int action = tag.doStartTag(); assertEquals(Tag.EVAL_BODY_INCLUDE, action); action = tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, action); assertEquals(formatter.print(new BigDecimal(".25"), Locale.getDefault()), ((MockHttpServletResponse) context.getResponse()).getContentAsString()); }
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) { String pattern = resolveEmbeddedValue(annotation.pattern()); if (StringUtils.hasLength(pattern)) { return new PatternDecoratingFormatter(pattern); } else { Style style = annotation.style(); if (style == Style.NUMBER) { return new NumberDecoratingFormatter(new NumberStyleFormatter()); } else if (style == Style.PERCENT) { return new NumberDecoratingFormatter(new PercentStyleFormatter()); } else { return new NumberDecoratingFormatter(new CurrencyStyleFormatter()); } } }
@Test(expected = ParseException.class) public void parseBogusValue() throws ParseException { formatter.parse("bogus", Locale.US); }
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) { String pattern = resolveEmbeddedValue(annotation.pattern()); if (StringUtils.hasLength(pattern)) { return new NumberStyleFormatter(pattern); } else { Style style = annotation.style(); if (style == Style.CURRENCY) { return new CurrencyStyleFormatter(); } else if (style == Style.PERCENT) { return new PercentStyleFormatter(); } else { return new NumberStyleFormatter(); } } }
@Test public void parseValue() throws ParseException { assertEquals(new BigDecimal(".2356"), formatter.parse("23.56%", Locale.US)); }
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) { String pattern = resolveEmbeddedValue(annotation.pattern()); if (StringUtils.hasLength(pattern)) { return new PatternDecoratingFormatter(pattern); } else { Style style = annotation.style(); if (style == Style.NUMBER) { return new NumberDecoratingFormatter(new NumberStyleFormatter()); } else if (style == Style.PERCENT) { return new NumberDecoratingFormatter(new PercentStyleFormatter()); } else { return new NumberDecoratingFormatter(new CurrencyStyleFormatter()); } } }
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) { String pattern = resolveEmbeddedValue(annotation.pattern()); if (StringUtils.hasLength(pattern)) { return new NumberStyleFormatter(pattern); } else { Style style = annotation.style(); if (style == Style.CURRENCY) { return new CurrencyStyleFormatter(); } else if (style == Style.PERCENT) { return new PercentStyleFormatter(); } else { return new NumberStyleFormatter(); } } }
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) { String pattern = resolveEmbeddedValue(annotation.pattern()); if (StringUtils.hasLength(pattern)) { return new PatternDecoratingFormatter(pattern); } else { Style style = annotation.style(); if (style == Style.NUMBER) { return new NumberDecoratingFormatter(new NumberStyleFormatter()); } else if (style == Style.PERCENT) { return new NumberDecoratingFormatter(new PercentStyleFormatter()); } else { return new NumberDecoratingFormatter(new CurrencyStyleFormatter()); } } }