Advertisement
Guest User

Untitled

a guest
May 28th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. # Number Formatter: how to use
  2.  
  3. ```js
  4. // some pre-made formatters
  5. var myFormatFn = NumberFormatter.PERCENT; // check NumberFormatter.js for more
  6. myFormatFn(0.54122); // '54.1%'
  7. myFormatFn('0.54122'); // '54.1%'
  8.  
  9. var formatter = new NumberFormatter();
  10.  
  11. var myStr = formatter.format('0.0', 12.23); // '12.2'
  12.  
  13. // callbacks?
  14. var myChartSettings = {
  15. yAxisFormatter: formatter.createCallback('0.0')
  16. };
  17.  
  18. // with a custom strategy
  19. var myStr = formatter.formatUsing(new CustomStrategy(), 12.984);
  20. ```
  21.  
  22. # CustomNumberFormatter
  23.  
  24. This subclass gives you more flexibility with pre and post process functions.
  25.  
  26. *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:
  27.  
  28. ```javascript
  29. var formatter = new CustomNumberFormatter(
  30. '0.0',
  31. value => value * 3.6, // Pre: value here is still the number
  32. value => value + ' km/h' // Post: value here is already the formatter str
  33. );
  34.  
  35. var myStr = formatter.format(10); // '36 km/h'
  36. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement