Banana.SDecimal

Questa documentazione è superata

La documentazione più completa e aggiornata è quella di Banana Contabilità Plus: Provalo subito

In this article

The Banana.SDecimal (String Decimal) provide functions to do decimal math calculation that

  • use decimal string in the form of '10.00' or '-10' as argument and return value
    • '.' is interpreted as the decimal separator
    • thousand separator are not allowed
  • has up to 34  digits of numeric precision
  • do accurate decimal rounding

You can use this functions instead of the javascript Number that use floating point arithmetic and are not very suitable for accounting calculation do the rounding differences.

var r = Banana.SDecimal.add('6.50', '3.50'); // return '10.00'
r = Banana.SDecimal.divide('10', '2'); // return '5.00'
r = Banana.SDecimal.divide('10', '2', ''); // return '5'
r = Banana.SDecimal.divide('10', '2'); // return '5.00000'

SDecimal has been Introduced in 7.0.7.0

Rounding context

Functions can be passed a rounding context that specify the rounding properties :

  • decimals (default 2) the number of decimal digit
  • mode the rounding mode (default is HALF_UP)
    • Banana.SDecimal.HALF_UP the amount are rounded to the nearest.
      The 0.5 are rounded up.
    • Banana.SDecimal.HALF_EVEN the amount are rounded to the nearest
      The 0.5 are rounded up or down based on the preceding digit.

if the rounding context is omitted no rounding is done.

Rounding context of the accounting file

All Banana document file have a rounding context tha can be retrieved with the function

"Banana.document.rounding" see the Banana.document..

Examples

// no context
var r = Banana.SDecimal.divide('10', '3'); // return '3.3333333333333333333333333'
var context = {'decimals' : 4, 'mode' : Banana.SDecimal.HALF_UP}
var r = Banana.SDecimal.divide('10', '3', context); // return '3.3333'
var r = Banana.SDecimal.divide('10', '3', {'decimals':0}); // return '3'

// (accunting file 2 decimals
r = Banana.SDecimal.divide('10', '3', Banana.document.rounding); // return '3.33'

Functions

abs( value1, [, rounding])

return the value1 without the sign and rounded as indicated

var r = Banana.SDecimal.abs('-10') // return '10.00'

add( value1, value2 [, rounding])

This fuction return the sum of value1 and value2

all parameters and the return value are javasctipt strings

roundingDecimals is the number of decimals

  • default value is 2,
  • valid values are 
    • null '' that return the value unrounded
    • '0' return with no decimals
    • '1' to '34' return the value with the indicated number o decimals
var r = Banana.SDecimal.add('6.50', '3.50'); // return '10.00'

compare( value1, value2 )

return an integer value

  • 1 if value1 > value2
  • 0 if value1 = value2
  • -1 if value1 < value2

divide( value1, value2 [, rounding])

return value1 divided by value2

var r = Banana.SDecimal.divide('6', '3'); // return '2.00'

isZero( value1)

return true if value1 is zero void, return false if value1 is not zero.

max( value1, value2 [, rounding])

return the max of value1 and value2

var r = Banana.SDecimal.max('6', '3'); // return '6.00'

min( value1, value2 [, rounding])

return the min of value1 and value2

var r = Banana.SDecimal.min('6', '3'); // return '3.00'

 

multiply( value1, value2 [, rounding])

return value1 multiplied by value2

var r = Banana.SDecimal.multiply('6', '3'); // return '18.00'

remainder ( value1, value2 [, rounding])

divide value1 by value2 and return the reminder

var r = Banana.SDecimal.reminder('10', '3'); // return '1.00'

round( value1, [, rounding])

return value1 round to the spcified rounding context

var r = Banana.SDecimal.round('6.123456'); // no context no rounding 
r = Banana.SDecimal.round('6.123456', {'decimals':2}); // return '6.12'

roundNearest( value1, nearest, [, rounding])

return value1 round to the specified minimal amount

var r = Banana.SDecimal.round('6.17', '0.1'); // return 6.1 
r = Banana.SDecimal.round('6.17', '0.05', {'decimals':2}); // return '6.15'

invert ( value, [, rounding])

If positive return a negative value, if negative return a positive value.

sign( value)

Return 1 if value > 0, 0 if  value = 0, -1 if value <0

subtract ( value1, value2 [, rounding])

subtract value2 from value1

var r = Banana.SDecimal.subtract('10', '3'); // return '7.00'

 

Locale conversion

To convert to and from the locale format use the Banana.Converter functions

  • Banana.Converter.toInternalNumberFormat(value [, decimals, convZero])

  • Banana.Converter.toLocaleNumberFormat(value [, decimalSeparator])

var sum = Banana.SDecimal.add('10000', '2000'); // return '12000.00'
var printValue = Banana.Converter.toLocaleNumberFormat(sum);