// some pre-made formatters
var myFormatFn = NumberFormatter.PERCENT; // check NumberFormatter.js for more
myFormatFn(0.54122); // '54.1%'
myFormatFn('0.54122'); // '54.1%'
var formatter = new NumberFormatter();
var myStr = formatter.format('0.0', 12.23); // '12.2'
// callbacks?
var myChartSettings = {
yAxisFormatter: formatter.createCallback('0.0')
};
// with a custom strategy
var myStr = formatter.formatUsing(new CustomStrategy(), 12.984);
This subclass gives you more flexibility with pre and post process functions.
Example: let's say you have an API that returns wind speed in meters/second and you want to format it as km/h with one decimal place:
var formatter = new CustomNumberFormatter(
'0.0',
value => value * 3.6, // Pre: value here is still the number
value => value + ' km/h' // Post: value here is already the formatter str
);
var myStr = formatter.format(10); // '36 km/h'