JSPM

toformat

2.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 208257
  • Score
    100M100P100Q189255F
  • License MIT

Adds a `toFormat` instance method to decimal.js or big.js

Package Exports

  • toformat

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (toformat) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

toFormat

Adds a toFormat instance method to big.js or decimal.js.

Install

Node.js

$ npm install toformat

Browser

<script src='path/to/big.js'></script>
<script src='path/to/toFormat.js'></script>

Use

Node.js

Big = require('big')
Big = require('toformat')(Big)

x = new Big(9876.54321)
x.toFormat(2)                       // '9,876.54'

// Three different ways of setting a formatting property
Big.format.decimalSeparator = ','
x.format.groupSeparator: ' '
x.toFormat(1, { groupSize: 2 })    // '98 76,5'

Browser

toFormat(Big)
x = new Big(9876.54321)
x.toFormat(2)                      // '9,876.54'

Further examples:

// The format object added to the Decimal constructor by this library.
Decimal.format = {
  decimalSeparator: '.',
  groupSeparator: ',',
  groupSize: 3,
  secondaryGroupSize: 0,
  fractionGroupSeparator: '',
  fractionGroupSize : 0
}

x.toFormat()                        // 123,456,789.987654321
x.toFormat(2, 1)                    // 123,456,789.98

// Add a format object to a Decimal instance.
x.format = {
  decimalSeparator: ',',
  groupSeparator: '',
}

x.toFormat()                       // 123456789,987654321

format = {
  decimalSeparator: '.',
  groupSeparator: ' ',
  groupSize: 3,
  fractionGroupSeparator: ' ',
  fractionGroupSize : 5
}

// Pass a format object to the method call.
x.toFormat(format)                 // 123 456 789.98765 4321
x.toFormat(4, format)              // 123 456 789.9877
x.toFormat(2, 1, format)           // 123 456 789.98

Test

$ npm test

Licence

MIT