Number manipulation

Formatters can be chained with dynamic parameters to create complex operations, check out the example in Dynamic parameters.

convCurr( target, source )

  • converts from one currency to another

  • exchange rates are included by default

    • you can provide a new exchange rate for one report in options.currencyRates of Carbone.render or globally with Carbone.set

  • convCurr() without parameters converts automatically from options.currencySource to options.currencyTarget

ParametersDescriptionType

target

  • [optional] convert to this currency ('EUR')

  • by default it equals options.currencyTarget

string

source

  • [optional] currency of source data ('USD')

  • by default it equals options.currencySource

string

Examples

// with options = {
//   "currency": {
//     "source": "EUR",
//     "target": "USD",
//     "rates": {
//       "EUR": 1,
//       "USD": 2
//     }
//   }
// }
10:convCurr() // 20
1000:convCurr() // 2000
1000:convCurr('EUR') // 1000
1000:convCurr('USD') // 2000
1000:convCurr('USD', 'USD') // 1000

round( precision )

  • rounds a number

  • same as toFixed() but it rounds number correctly round(1.05, 1) = 1.1

ParametersDescriptionType

precision

number of decimal

number

Examples

10.05123:round(2) // 10.05
1.05:round(1) // 1.1

formatN( precision )

  • formats number according to the locale

  • applying a number of decimals depends on the report type:

    • for ODS/XLSX, the number of decimals has to be formatted based on the text editor

    • for the other type of files, the number of decimals depends on the precision parameter passed to the formatter

ParametersDescriptionType

precision

[optional] number of decimals

number

Examples

// with options = {
//   "lang": "en-us"
// }
'10':formatN() // "10.000"
'1000.456':formatN() // "1,000.456"

formatC( precisionOrFormat )

ParametersDescriptionType

precisionOrFormat

[optional] number of decimal, or specific format

  • integer : change default precision of the currency

  • M : print major currency name without the number

  • L : prints number with currency symbol (by default)

  • LL : prints number with major currency name

number

Examples

// with options = {
//   "lang": "en-us",
//   "currency": {
//     "source": "EUR",
//     "target": "USD",
//     "rates": {
//       "EUR": 1,
//       "USD": 2
//     }
//   }
// }
'1000.456':formatC() // "$2,000.91"
'1000.456':formatC('M') // "dollars"
'1':formatC('M') // "dollar"
'1000':formatC('L') // "$2,000.00"
'1000':formatC('LL') // "2,000.00 dollars"
// with options = {
//   "lang": "fr-fr",
//   "currency": {
//     "source": "EUR",
//     "target": "USD",
//     "rates": {
//       "EUR": 1,
//       "USD": 2
//     }
//   }
// }
'1000.456':formatC() // "2 000,91 --TMPL-4-MARK--quot;
// with options = {
//   "lang": "fr-fr",
//   "currency": {
//     "source": "EUR",
//     "target": "EUR",
//     "rates": {
//       "EUR": 1,
//       "USD": 2
//     }
//   }
// }
'1000.456':formatC() // "1 000,46 €"

add( )

Adds 2 numbers.

Examples

1000.4:add(2) // 1002.4
'1000.4':add('2') // 1002.4

sub( )

Subtracts 2 numbers.

Examples

1000.4:sub(2) // 998.4
'1000.4':sub('2') // 998.4

mul( )

Multiplies 2 numbers.

Examples

1000.4:mul(2) // 2000.8
'1000.4':mul('2') // 2000.8

div( )

Divides 2 numbers.

Examples

1000.4:div(2) // 500.2
'1000.4':div('2') // 500.2

int( )

Converts a number to an INT.

toEN( )

Converts a number with English specifications (decimal separator is a dot .).

toFixed( )

Converts a number into string, keeping only decimals.

toFR( )

Converts a number with French specifications (decimal separator is a comma ,).

Last updated