number formatting back and forth
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
- 491
|
- 491
|
||||||
- 501
|
- 501
|
||||||
- upgraded en16931 cen schematron to 1.3.12
|
- upgraded en16931 cen schematron to 1.3.12
|
||||||
|
- -499/500 PDF layout corrections
|
||||||
|
|
||||||
2.14.0
|
2.14.0
|
||||||
=======
|
=======
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public class XMLTools extends XMLWriter {
|
|||||||
|
|
||||||
public static String nDigitFormat(BigDecimal value, int scale) {
|
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
|
* NumberFormat.getCurrencyInstance().format( 12345.6789 ); but that is locale
|
||||||
* specific.I also tried DecimalFormat df = new DecimalFormat( "0,00" );
|
* specific.I also tried DecimalFormat df = new DecimalFormat( "0,00" );
|
||||||
* df.setDecimalSeparatorAlwaysShown(true); df.setGroupingUsed(false);
|
* df.setDecimalSeparatorAlwaysShown(true); df.setGroupingUsed(false);
|
||||||
@@ -39,7 +39,28 @@ public class XMLTools extends XMLWriter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String encodeXML(CharSequence s) {
|
/***
|
||||||
|
* 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) {
|
if (s == null) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class LineCalculator {
|
|||||||
BigDecimal multiplicator = vatPercent.divide(BigDecimal.valueOf(100));
|
BigDecimal multiplicator = vatPercent.divide(BigDecimal.valueOf(100));
|
||||||
priceGross = currentItem.getPrice(); // see https://github.com/ZUGFeRD/mustangproject/issues/159
|
priceGross = currentItem.getPrice(); // see https://github.com/ZUGFeRD/mustangproject/issues/159
|
||||||
price = priceGross.subtract(allowance).add(charge);
|
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);
|
.subtract(allowanceItemTotal).setScale(2, RoundingMode.HALF_UP);
|
||||||
itemTotalVATAmount = itemTotalNetAmount.multiply(multiplicator);
|
itemTotalVATAmount = itemTotalNetAmount.multiply(multiplicator);
|
||||||
|
|
||||||
|
|||||||
@@ -76,11 +76,13 @@ public class ZUGFeRD2PullProvider implements IXMLProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected String priceFormat(BigDecimal value) {
|
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) {
|
protected String quantityFormat(BigDecimal value) {
|
||||||
return XMLTools.nDigitFormat(value, 18);
|
return XMLTools.nDigitFormatDecimalRange(value, 18, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -52,6 +52,15 @@ public class BaseTest extends TestCase {
|
|||||||
assertEquals("12.00", XMLTools.nDigitFormat(new BigDecimal("12"),2));
|
assertEquals("12.00", XMLTools.nDigitFormat(new BigDecimal("12"),2));
|
||||||
assertEquals("12", XMLTools.nDigitFormat(new BigDecimal("12"),0));
|
assertEquals("12", XMLTools.nDigitFormat(new BigDecimal("12"),0));
|
||||||
assertEquals("20000123.342", XMLTools.nDigitFormat(new BigDecimal("20000123.3419"),3));
|
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));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -189,7 +189,7 @@ public class CalculationTest {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* LineCalculator should not throw an exception when calculating a non-terminating decimal expansion
|
* LineCalculator should not throw an exception when calculating a non-terminating decimal expansion
|
||||||
*/
|
* */
|
||||||
@Test
|
@Test
|
||||||
public void testNonTerminatingDecimalExpansion() {
|
public void testNonTerminatingDecimalExpansion() {
|
||||||
final Product product = new Product();
|
final Product product = new Product();
|
||||||
|
|||||||
@@ -297,7 +297,8 @@ public class ZF2EdgeTest extends MustangReaderTestCase {
|
|||||||
InputStream SOURCE_PDF = this.getClass()
|
InputStream SOURCE_PDF = this.getClass()
|
||||||
.getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf");
|
.getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf");
|
||||||
|
|
||||||
ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1();ze.ignorePDFAErrors();
|
ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1();
|
||||||
|
ze.ignorePDFAErrors();
|
||||||
ze.load(SOURCE_PDF);
|
ze.load(SOURCE_PDF);
|
||||||
ze.setProducer("My Application")
|
ze.setProducer("My Application")
|
||||||
.setCreator(System.getProperty("user.name")).setZUGFeRDVersion(2).setProfile(Profiles.getByName("Extended"));
|
.setCreator(System.getProperty("user.name")).setZUGFeRDVersion(2).setProfile(Profiles.getByName("Extended"));
|
||||||
|
|||||||
Reference in New Issue
Block a user