in Code

My weekend project: accounting.js

accounting.js is a tiny JavaScript library for number, money and currency formatting, with optional excel-style column rendering (to line up symbols and decimals). It’s lightweight, fully localisable and has zero dependencies.

I decided to take the couple of number formatting functions I posted last week and turn them into a library, with the key addition being the excel-style column formatting, which takes an array of values and adds padding between the currency symbol and the number, so that the symbols and decimal places are aligned:

Original: With accounting.js:
123.5 $ 123.50
3456.49 $ 3,456.49
777888.99 $ 777,888.99
-5432 $ -5,432.00
// Format list of numbers for display:
accounting.formatColumn([123.5, 3456.49, 777888.99, 12345678, -5432], "$ ");

It also includes functions for formatting any value to currency with custom precision and symbols, and a function to remove all currency formatting:

// Default usage and custom precision/symbol :
accounting.formatMoney(12345678); // $12,345,678.00
accounting.formatMoney(4999.99, "€", 2, ".", ",")); // €4.999,99
accounting.formatMoney(-500000, "£ ", 0) ); // £ -500,000

// Basic number formatting:
accounting.formatNumber(9876543); // 9,876,543 

// Remove formatting:
accounting.unformat("GBP £ 12,345,678.90"); // 12345678.9

It’s early days, but check out the accounting.js homepage for more info and live demos, and watch the github repository for the next version.

Write a Comment

Comment

  1. Hi Joss, Thanks heaps for publishing this, it’s amazing – compact and simple to use. We’ve just started using it in a financial reporting app and it fits the bill perfectly.

    When we’re live we’ll be linking back here!

    • Hey, that’s great to hear! Thanks.

      There are some upgrades on the way and hopefully most of them will be informed by the people using the library (e.g. you) – so if you find and issues, please report them on the github repository.

      I love your site btw – make sure to send me a link so I can add it to the showcase, when you have something live!

  2. Thanks for the very handy library. Easy to use, and easy to install and set-up defaults. I can tell you didn’t just make something for yourself, but put in the extra time needed to make it quickly usable by others. This was thoughtful and generous of you. Thanks.

    • Thanks, glad you like it and really nice of you to say so!

      If you’re using it on a live website somewhere, feel free to shoot me a link and I’ll add it to the eventual ‘showcase’.