@Override public String format(@Nullable final MonetaryAmount price) { return Optional.ofNullable(price).map(p -> { final boolean isDecimal = p.getNumber().doubleValueExact() % 1 != 0;//TODO this can be improved with monetary query final AmountFormatQuery pattern = AmountFormatQueryBuilder.of(locale) .set(CurrencyStyle.SYMBOL) .set(PATTERN, isDecimal ? PATTERN_WITH_DECIMAL : PATTERN_WITHOUT_DECIMAL) .build(); return MonetaryFormats.getAmountFormat(pattern).format(p); }).orElse(""); } }
/** * Check if a correct double value is returned, no truncation is * allowed to be performed. */ @SpecAssertion(section = "4.2.3", id = "423-A4") @Test(description = "4.2.3 Ensure NumberValue doubleValue(), doubleValueExact() provide correct values.") public void testValidDouble() { double[] nums = new double[]{0, 0.3, 1, 1.3453}; for (double num : nums) { for (Class type : Monetary.getAmountTypes()) { if (type.equals(TestAmount.class)) { continue; } MonetaryAmount mAmount1 = Monetary.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(num).create(); NumberValue result = mAmount1.getNumber(); AssertJUnit.assertNotNull("Section 4.2.3: Amount creation failed for " + type, result); AssertJUnit.assertEquals("Section 4.2.3: Number value (double) returned is not correct for " + type.getName(), num, result.doubleValue(), 0d); AssertJUnit .assertEquals("Section 4.2.3: Exact number value (double) returned is not correct for " + type.getName(), num, result.doubleValueExact(), 0d); } } }