/** * Formats the model to markup instead of to plain text if the result markup will be more than just plain text * escaped, otherwise falls back to formatting to plain text. If the markup result would be just the result of * {@link #formatToPlainText(TemplateNumberModel)} escaped, it must return the {@link String} that * {@link #formatToPlainText(TemplateNumberModel)} does. * * <p> * The implementation in {@link TemplateNumberFormat} simply calls {@link #formatToPlainText(TemplateNumberModel)}. * * @return A {@link String} or a {@link TemplateMarkupOutputModel}; not {@code null}. */ public Object format(TemplateNumberModel numberModel) throws TemplateValueFormatException, TemplateModelException { return formatToPlainText(numberModel); }
/** * Format number with the number format specified as the parameter, with the current locale. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText( TemplateNumberModel number, TemplateNumberFormat format, Expression exp, boolean useTempModelExc) throws TemplateException { try { return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(number)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, useTempModelExc); } }
/** * Formats the model to markup instead of to plain text if the result markup will be more than just plain text * escaped, otherwise falls back to formatting to plain text. If the markup result would be just the result of * {@link #formatToPlainText(TemplateNumberModel)} escaped, it must return the {@link String} that * {@link #formatToPlainText(TemplateNumberModel)} does. * * <p> * The implementation in {@link TemplateNumberFormat} simply calls {@link #formatToPlainText(TemplateNumberModel)}. * * @return A {@link String} or a {@link TemplateMarkupOutputModel}; not {@code null}. */ public Object format(TemplateNumberModel numberModel) throws TemplateValueFormatException, TemplateModelException { return formatToPlainText(numberModel); }
/** * Formats the model to markup instead of to plain text if the result markup will be more than just plain text * escaped, otherwise falls back to formatting to plain text. If the markup result would be just the result of * {@link #formatToPlainText(TemplateNumberModel)} escaped, it must return the {@link String} that * {@link #formatToPlainText(TemplateNumberModel)} does. * * <p> * The implementation in {@link TemplateNumberFormat} simply calls {@link #formatToPlainText(TemplateNumberModel)}. * * @return A {@link String} or a {@link TemplateMarkupOutputModel}; not {@code null}. */ public Object format(TemplateNumberModel numberModel) throws TemplateValueFormatException, TemplateModelException { return formatToPlainText(numberModel); }
/** * Format number with the number format specified as the parameter, with the current locale. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText( TemplateNumberModel number, TemplateNumberFormat format, Expression exp, boolean useTempModelExc) throws TemplateException { try { return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(number)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, useTempModelExc); } }
/** * Format number with the number format specified as the parameter, with the current locale. * * @param exp * The blamed expression if an error occurs; it's only needed for better error messages */ String formatNumberToPlainText( TemplateNumberModel number, TemplateNumberFormat format, Expression exp, boolean useTempModelExc) throws TemplateException { try { return EvalUtil.assertFormatResultNotNull(format.formatToPlainText(number)); } catch (TemplateValueFormatException e) { throw _MessageUtil.newCantFormatNumberException(format, exp, e, useTempModelExc); } }
@Test void formattingEnglish() throws TemplateValueFormatException, TemplateModelException { final BigDecimal n = new BigDecimal("0.0001"); final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Locale.ENGLISH, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> n; final Object result = f.formatToPlainText(m); assertThat(result).isEqualTo("0.01%"); }
@Test void formattingEnglishRoundingEvenUp() throws TemplateValueFormatException, TemplateModelException { final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Locale.ENGLISH, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> new BigDecimal("0.00055"); final Object result = f.formatToPlainText(m); assertThat(result).isEqualTo("0.06%"); }
@Test void formattingEnglishLongZero() throws TemplateValueFormatException, TemplateModelException { final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Locale.ENGLISH, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> new BigDecimal("0.000000000000000000"); final Object result = f.formatToPlainText(m); assertThat(result).isEqualTo("0.00%"); }
@DisabledOnJre(JRE.JAVA_8) @Test void formattingCzech3() throws TemplateValueFormatException, TemplateModelException { final BigDecimal n = new BigDecimal("0.001"); final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Defaults.LOCALE, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> n; final String result = f.formatToPlainText(m); assertThat(result.trim()).isEqualTo("0,10" + (char) 160 + "%"); }
@Test void formattingEnglishZero() throws TemplateValueFormatException, TemplateModelException { final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Locale.ENGLISH, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> BigDecimal.ZERO; final Object result = f.formatToPlainText(m); assertThat(result).isEqualTo("0.00%"); }
@DisabledOnJre(JRE.JAVA_8) @Test void formattingCzech() throws TemplateValueFormatException, TemplateModelException { final BigDecimal n = new BigDecimal("0.0001"); final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Defaults.LOCALE, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> n; final String result = f.formatToPlainText(m); assertThat(result.trim()).isEqualTo("0,01" + (char) 160 + "%"); }
@Test void formattingEnglishRoundingUp() throws TemplateValueFormatException, TemplateModelException { final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Locale.ENGLISH, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> new BigDecimal("0.00049"); final Object result = f.formatToPlainText(m); assertThat(result).isEqualTo("0.05%"); }
@Test void formattingEnglishRoundingEvenDown() throws TemplateValueFormatException, TemplateModelException { final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Locale.ENGLISH, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> new BigDecimal("0.00025"); final Object result = f.formatToPlainText(m); assertThat(result).isEqualTo("0.02%"); }
@Test void formattingEnglishRoundingDown() throws TemplateValueFormatException, TemplateModelException { final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Locale.ENGLISH, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> new BigDecimal("0.00051"); final Object result = f.formatToPlainText(m); assertThat(result).isEqualTo("0.05%"); }
@DisabledOnJre(JRE.JAVA_8) @Test void formattingCzech2() throws TemplateValueFormatException, TemplateModelException { final BigDecimal n = new BigDecimal("0.0000"); final TemplateNumberFormat f = InterestNumberFormatFactory.INSTANCE.get("", Defaults.LOCALE, Environment.getCurrentEnvironment()); final TemplateNumberModel m = () -> n; final String result = f.formatToPlainText(m); assertThat(result.trim()).isEqualTo("0,00" + (char) 160 + "%"); }