number formatting back and forth

This commit is contained in:
jstaerk
2024-10-05 17:49:07 +02:00
parent 0004d9e982
commit 0967971e16
7 changed files with 41 additions and 7 deletions

View File

@@ -4,6 +4,7 @@
- 491
- 501
- upgraded en16931 cen schematron to 1.3.12
- -499/500 PDF layout corrections
2.14.0
=======

View File

@@ -21,7 +21,7 @@ public class XMLTools extends XMLWriter {
public static String nDigitFormat(BigDecimal value, int scale) {
/*
* I needed 123,45, locale independent.I tried
* I needed 123.45, locale independent.I tried
* NumberFormat.getCurrencyInstance().format( 12345.6789 ); but that is locale
* specific.I also tried DecimalFormat df = new DecimalFormat( "0,00" );
* df.setDecimalSeparatorAlwaysShown(true); df.setGroupingUsed(false);
@@ -39,6 +39,27 @@ public class XMLTools extends XMLWriter {
}
/***
* formats a number so that at least minDecimals are displayed but at the maximum maxDecimals are there, i.e.
* cuts potential 0s off the end until minDecimals
* @param value
* @param maxDecimals number of maximal scale
* @param minDecimals number of minimal scale
* @return value as String with decimals in the specified range
*/
public static String nDigitFormatDecimalRange(BigDecimal value, int maxDecimals, int minDecimals) {
if ((maxDecimals<minDecimals)||(maxDecimals<0)||(minDecimals<0)) {
throw new IllegalArgumentException("Invalid scale range provided");
}
int curDecimals=maxDecimals;
while ( (curDecimals>minDecimals) && (value.setScale(curDecimals, RoundingMode.HALF_UP).compareTo(value.setScale(curDecimals-1, RoundingMode.HALF_UP))==0)) {
curDecimals--;
}
return value.setScale(curDecimals, RoundingMode.HALF_UP).toPlainString();
}
public static String encodeXML(CharSequence s) {
if (s == null) {
return "";

View File

@@ -40,7 +40,7 @@ public class LineCalculator {
BigDecimal multiplicator = vatPercent.divide(BigDecimal.valueOf(100));
priceGross = currentItem.getPrice(); // see https://github.com/ZUGFeRD/mustangproject/issues/159
price = priceGross.subtract(allowance).add(charge);
itemTotalNetAmount = currentItem.getQuantity().multiply(getPrice()).divide(currentItem.getBasisQuantity(), RoundingMode.HALF_UP)
itemTotalNetAmount = currentItem.getQuantity().multiply(getPrice()).divide(currentItem.getBasisQuantity(), 18, RoundingMode.HALF_UP)
.subtract(allowanceItemTotal).setScale(2, RoundingMode.HALF_UP);
itemTotalVATAmount = itemTotalNetAmount.multiply(multiplicator);

View File

@@ -76,11 +76,13 @@ public class ZUGFeRD2PullProvider implements IXMLProvider {
}
protected String priceFormat(BigDecimal value) {
return XMLTools.nDigitFormat(value, 18);
// 18 decimals are max for price and qty due to xml restrictions,
// see Chapter 3.2.3 of https://www.w3.org/TR/xmlschema-2/
return XMLTools.nDigitFormatDecimalRange(value, 18, 4);
}
protected String quantityFormat(BigDecimal value) {
return XMLTools.nDigitFormat(value, 18);
return XMLTools.nDigitFormatDecimalRange(value, 18, 4);
}
@Override

View File

@@ -52,6 +52,15 @@ public class BaseTest extends TestCase {
assertEquals("12.00", XMLTools.nDigitFormat(new BigDecimal("12"),2));
assertEquals("12", XMLTools.nDigitFormat(new BigDecimal("12"),0));
assertEquals("20000123.342", XMLTools.nDigitFormat(new BigDecimal("20000123.3419"),3));
assertEquals("0.00", XMLTools.nDigitFormatDecimalRange(BigDecimal.ZERO,2, 2));
assertEquals("-1.10", XMLTools.nDigitFormatDecimalRange(new BigDecimal("-1.100000"), 4,2));
assertEquals("-1.101", XMLTools.nDigitFormatDecimalRange(new BigDecimal("-1.101000"),10, 3));
assertEquals("-1.10", XMLTools.nDigitFormatDecimalRange(new BigDecimal("-1.103"), 2,2));
assertEquals("4", XMLTools.nDigitFormatDecimalRange(new BigDecimal("4"),2, 0));
assertEquals("3.14", XMLTools.nDigitFormatDecimalRange(new BigDecimal("3.141526"),2, 0));
}
}

View File

@@ -189,7 +189,7 @@ public class CalculationTest {
/**
* LineCalculator should not throw an exception when calculating a non-terminating decimal expansion
*/
* */
@Test
public void testNonTerminatingDecimalExpansion() {
final Product product = new Product();

View File

@@ -297,7 +297,8 @@ public class ZF2EdgeTest extends MustangReaderTestCase {
InputStream SOURCE_PDF = this.getClass()
.getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf");
ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1();ze.ignorePDFAErrors();
ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1();
ze.ignorePDFAErrors();
ze.load(SOURCE_PDF);
ze.setProducer("My Application")
.setCreator(System.getProperty("user.name")).setZUGFeRDVersion(2).setProfile(Profiles.getByName("Extended"));