venerdì, novembre 05, 2010

JavaScript: Formatting Numbers for Dollars and Cents

More JavaScript goodness. This time from pageresource.com: Number.toFixed(x).

“Formats any number for "x" number of trailing decimals. The number is rounded up, and "0"s are used after the decimal point if needed to create the desired decimal length.”

Here’s a function I’m using to format several numbers on the fly.

function addDecimalPlaces(myCash) {
       myCash = myCash * 1; // Confirm that myCash is a number
       myCash = myCash.toFixed(2);
       return myCash;
}