Class EinvoiceDecimal() Professional

Exact fixed-point decimal for amounts, quantities and rates.

Description

The EinvoiceDecimal class implements the exact decimal number type used for all amounts, quantities, prices and percentages which the e-invoice classes calculate or return. The business rules of EN 16931, for example BR-CO-10, BR-CO-13, BR-CO-15 and BR-CO-17, require sums to match exactly to the cent. The numeric type of Xbase++ is an IEEE 754 double precision floating point number, which cannot represent most decimal fractions exactly. The EinvoiceDecimal class avoids this problem by storing the value as an integer scaled by 10^8, which provides 8 decimal digits of internal precision.

An EinvoiceDecimal object is normally created via EinvoiceDecimal():of(). All setter methods of the e-invoice classes which accept an amount, a quantity, a price or a rate, for example EinvoiceLineItem():setPrice() and EinvoicePaymentTerms():addSkonto(), accept a numeric value or a character string and convert it automatically via EinvoiceDecimal():of(). The corresponding getter methods, and all amounts contained in the invoice model returned by AbstractEinvoice():toModel(), for example Totals:GrandTotal or the members :Basis and :Tax of a VAT breakdown group, return an EinvoiceDecimal object instead of a numeric value.

The value of an EinvoiceDecimal object is never changed after the object has been created. The arithmetic methods :add(), :sub(), :mul(), :div(), :neg(), :abs() and :round() return a new EinvoiceDecimal object with the result and leave the original object unchanged.

The methods :toFixed( <nDp> ) and :toPlain( <nMinDp>, <nMaxDp> ) convert the value to a character string, and :toNumber() converts it to a numeric value. The methods :cmp(), :eq(), :isZero() and :isNegative() compare values.

Note: the Xbase++ arithmetic operators cannot be used with EinvoiceDecimal objects. Use the arithmetic methods of the class instead.

Class Methods
:hundred()
Creates an EinvoiceDecimal object with the value 100.
:of()
Creates an EinvoiceDecimal object from a value.
:sum()
Calculates the sum of an array of values.
:zero()
Creates an EinvoiceDecimal object with the value zero.
Methods
:abs()
Returns the absolute value.
:add()
Adds a value.
:cmp()
Compares the value with another value.
:div()
Divides by a value.
:eq()
Checks whether the value equals another value.
:isNegative()
Checks whether the value is negative.
:isZero()
Checks whether the value is zero.
:mul()
Multiplies by a value.
:neg()
Negates the value.
:round()
Rounds the value to a number of decimal places.
:sub()
Subtracts a value.
:toFixed()
Converts the value to a string with fixed decimals.
:toNumber()
Converts the value to a numeric value.
:toPlain()
Converts the value to a string without trailing zeros.
:toString()
Returns the default string representation.
Examples
Read a calculated total
LOCAL oModel 

oModel := oInvoice:toModel() 
? "Grand total:", oModel:Totals:GrandTotal:toFixed(2), oModel:Currency 

Create a value from user input and calculate with it
LOCAL oPrice, oTotal 

oPrice := EinvoiceDecimal():of( "19.99" ) 
oTotal := oPrice:mul( EinvoiceDecimal():of( 3 ) ):round(2) 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.