JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1826
  • Score
    100M100P100Q115853F
  • License ISC

numeral-format

Package Exports

  • numerify
  • numerify/lib/index.es

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 (numerify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

download version language License

A tool for format number more convient, from a fucking cool project named Numeral , and smaller than it.

Install

npm i numerify -S

Start

npm

import numerify from 'numerify'
// commonjs package is 'numerify/lib/index.cjs.js'
console.log(numerify(1234).format('0,0'))

cdn

<script src="https://unpkg.com/numerify/lib/index.umd.min.js"></script>
<script>
console.log(numerify(1234).format('0,0'))
</script>

Format List

Number Format String
10000 0,0.0000 10,000.0000
10000.23 0,0 10,000
10000.23 +0,0 +10,000
-10000 0,0.0 -10,000.0
10000.1234 0.000 10000.123
100.1234 00000 00100
1000.1234 000000,0 001,000
10 000.00 010.00
10000.1234 0[.]00000 10000.12340
-10000 (0,0.0000) (10,000.0000)
-0.23 .00 -.23
-0.23 (.00) (.23)
0.23 0.00000 0.23000
0.23 0.0[0000] 0.23
1230974 0.0a 1.2m
1460 0 a 1 k
-104000 0a -104k
1 0% 100%
0.974878234 0.000% 97.488%
-0.43 0 % -43 %
0.43 (0.000 %) 43.000 %

Methods

numerify(number).format(formatter, roundFunction)

  • formatter default is '0,0'
  • roundFunction default is Math.round

numeral.setOptions

this default options is:

{
  zeroFormat: null,
  nullFormat: null,
  defaultFormat: '0,0',
  scalePercentBy100: true,
  abbrLabel: {
    th: 'k',
    mi: 'm',
    bi: 'b',
    tr: 't'
  }
}

in order to edit it, you can code such as

numerify.setOptions({
  zeroFormat: 'N/A',
  nullFormat: 'N/A',
  defaultFormat: '0,0',
  scalePercentBy100: true,
  abbrLabel: {
    th: 'k',
    mi: 'm',
    bi: 'B',
    tr: 'T'
  }
})
numeral(0).format('0.0') // N/A

numeral.register

Adding your own custom formats is as easy as adding a locale.

numerify.register('percentage', {
  regexps: { format: /(%)/ },
  format (value, format, roundingFunction) {
    const space = ~format.indexOf(' %') ? ' ' : ''
    let output

    if (numerify.options.scalePercentBy100) value = value * 100

    format = format.replace(/\s?%/, '')

    output = numerify.numberToFormat(value, format, roundingFunction)

    if (~output.indexOf(')')) {
      output = output.split('')
      output.splice(-1, 0, space + '%')
      output = output.join('')
    } else {
      output = output + space + '%'
    }

    return output
  }
})

License

MIT