Tag Archive for 'javascript'

JavaScript Number Formatting Library v1.3 released

Download Number Formatting Library

I’ve updated my JavaScript Number Formatting Library to version 1.3. This release adds the ability to customize how not-a-number (NaN), positive infinity and negative infinity are formatted. All you need to do is set the appropriate constant in Number.prototype:

  • Number.prototype.NaN
  • Number.prototype.posInfinity
  • Number.prototype.negInfinity

For more documentation, see the original article on JavaScript number formatting.

Technorati Tags:, , , , , ,

You might also like:

  1. How to format numbers in JavaScript flexibly and efficiently
  2. JavaScript number-formatting library updated
  3. Javascript date parsing and formatting, Part 2
  4. JavaScript formatting library update
  5. JavaScript date parsing and formatting, Part 1

JavaScript formatting library update

This is a quick update on the state of my JavaScript date formatting libraries and date chooser, and JavaScript number formatting library. It’s been a while since I wrote them, and as you can tell my interests have turned to many other things, but thet remain the best JavaScript formatting and parsing libraries I’ve seen.

I originally started this post in May of 2006, intending to use the libraries to demonstrate how HTML tables can contain multi-dimensional data, and use the seldom-used HTML elements like TFOOT to generate aggregate data about the table. This was going to be the follow-up to my tables and data with CSS post. I had a rough draft sketched out somewhere: a table full of numbers, dates, currencies and strings. A drop-down menu and a “format paintbrush” would let you reformat it all on the fly, and it would all be generated from semantic information attached to the table cells, not hard-coded into the page.

This was only practical because of the efficiency of my libraries; to reformat entire date regions in the table in real-time, for example, you’d need to parse the value as a date in one format, then reformat it for output in another. It was to be a showcase of how much efficiency matters for some things.

Tangent: I suppose it’s less important for people who aren’t still running 500MHz laptops these days, but efficiency really matters for me; a lot of these flashy sites these days simply take too much CPU for my little old computer to run well. I stubbornly resist getting a new computer because I cringe at the thought of the environmental cost, but I’m slowly breaking down; it’s gotten to the point my battery won’t charge, and Dell doesn’t even have a record of my service tag anymore. Spare parts for these things are long since unavailable.

Now I’m involved with quite different things, since I’m working more in programming and less in the Internet space. The good news is others keep reading and using all of my work — not just the recent work — which makes me happy. Just the other day Liran Tal wrote to tell me he’s using my Javascript libraries in the Daloradius project (check it out, it’s pretty cool). The date-parsing library found its way into some ExtJS tools that extend the YUI libraries, too.

And a few days ago someone sponsored an improvement to the number-formatting libraries.

Who knows — someday I may end up building some browser GUI systems again and use these. In the meantime it’s encouraging that they remain useful to people.

Note: This episode is pre-recorded. I’m taking a short hiatus from blogging and will respond to your comments when I return.

Technorati Tags:, , , , , , ,

You might also like:

  1. Javascript date parsing and formatting, Part 2
  2. JavaScript date formatting benchmarks
  3. JavaScript Number Formatting Library v1.3 released
  4. How to format numbers in JavaScript flexibly and efficiently
  5. JavaScript date chooser

JavaScript number-formatting library updated

Download number-functions

I’ve released a new version of my powerful, flexible, efficient JavaScript number-formatting library, which is probably the best available. This release adds a fix for zero-padding negative numbers.

If you find bugs, please send me test cases I can use to reproduce and add to the unit test suite. One test per line, like “input”, “format”, “expected” is best. For example, this is a great test case:

-1, "#,#.00", "-1.00"

I can plug that directly into the unit test suite, run it, and if it gives back “-01.00″ it will fail the test. This makes it much easier and more convenient for me to fix bugs.

Sponsoring bug fixes wouldn’t hurt either ;-)

Technorati Tags:, , , , , , , ,

You might also like:

  1. JavaScript Number Formatting Library v1.3 released
  2. JavaScript formatting library update
  3. How to format numbers in JavaScript flexibly and efficiently
  4. Javascript date parsing and formatting, Part 2
  5. How to write unit tests for ease of refactoring

How to format numbers in JavaScript flexibly and efficiently

Download number-functions

This article continues my series on parsing and formatting data with JavaScript, this time with numeric data. I don’t need to do number parsing, but formatting is very useful. The technique is similar to my date formatting code — code that writes code (for raw speed), using custom format specifier strings (for flexibility and ease of use). The result is number formatting functionality that is highly efficient, flexible, and easy to use.

First, the idea: you have a number, you want it formatted a certain way. Here’s how:

var dollars = 5.001;
alert(dollars.numberFormat("$0.00");
// result: "$5.00"
var percent = .08134;
alert(percent.numberFormat("0.00%");
// result: "8.13%"
var bignum = 12831242485472;
alert(bignum.numberFormat("0,0,, million");
// result: "12,831,243 million"

My custom date formatting code used PHP’s date-formatting syntax because it’s much less context-sensitive and (I think) more useful than Microsoft’s, but my number-formatting syntax is similar to Microsoft’s because it’s much more widely used and I don’t see an existing, better alternative. Rather than documenting it separately, I’ll just point you to the (poor quality) Microsoft documentation for the .NET Custom Numeric Format Strings functionality, and list the differences from my implementation:

  • Rounding works differently in multi-section format strings. In .NET with a two-section string,

    If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, then the resulting zero is formatted according to the first section.

    This is not true in my code — the number is formatted according to its value, and once the code decides which section applies, that section will be used no matter what happens during rounding.
  • Question marks are digit placeholders just like the number sign (#), but if there’s no digit to insert, they get replaced with spaces, not removed. They can be used for space-padding, which might be useful for, say, accounting notation.
  • You don’t have to enter quotes around strings that should be mixed in with the number placeholders. In fact, my syntax is much more permissive than the Microsoft syntax: anything can go anywhere. You can put arbitrary strings smack in the middle of your number if you want.
  • It’s not internationalized.

I’ve only implemented a subset of the various number-formatting syntaxes I’ve seen in spreadsheets and so forth. The subset is about 85% complete in my opinion. However, I think it’s functionally about 99% complete, which means I think 99% of the time you want to format a number, it will do what you want. The tradeoff is simplicity and speed. Number formatting is actually much more difficult than date formatting, and I’ve tried to keep the code sane.

I have a set of unit tests, which use the excellent JsUnit library. Bring up the unit test page and enter the following url to be tested: www.xaprb.com/articles/number-test.html.

Of course there’s the obligatory demo page, too.

Technorati Tags:, , , , ,

You might also like:

  1. Javascript date parsing and formatting, Part 2
  2. JavaScript date parsing and formatting, Part 1
  3. Excel vs. OpenOffice.org Calc in number formatting
  4. JavaScript formatting library update
  5. JavaScript Number Formatting Library v1.3 released