The accounting.js module can be used to parse strings into numeric values as we saw earlier in this chapter. It is also very good at preparing numbers for display and provides a number of functions to help with this.
In this recipe, we will look at various methods to display numbers using accounting.js.
Before we begin, let's make sure the accounting.js NPM module is installed:
npm install accounting --save
In this example, we will demonstrate ways to use accounting.js to format numbers:
Load the
accounting
module:accounting = require 'accounting'
Use the
formatMoney()
function:# formatting as currency (default formatting) console.log accounting.formatMoney 31415.9535 console.log accounting.formatMoney([100, 200, 300])
This will produce the following output:
$31,415.95 [ '$100.00', '$200.00', '$300.00' ]
Use the
formatNumber()
function in a similar way:# format as number console.log accounting.formatNumber 31415.9535 console...