Book Image

CoffeeScript Application Development Cookbook

By : Mike Hatfield
Book Image

CoffeeScript Application Development Cookbook

By: Mike Hatfield

Overview of this book

Table of Contents (18 chapters)
CoffeeScript Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Formatting numbers with accounting.js


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.

Getting ready

Before we begin, let's make sure the accounting.js NPM module is installed:

npm install accounting --save

How to do it...

In this example, we will demonstrate ways to use accounting.js to format numbers:

  1. Load the accounting module:

    accounting = require 'accounting'
    
  2. 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' ]
    
  3. Use the formatNumber() function in a similar way:

    # format as number
    console.log accounting.formatNumber 31415.9535
    console...