The easiest and most proper way to format a number as a currency value is to use the Java Script Number object's toFixed method. The toFixed method will only work in newer browsers like Internet Explorer version 5.5 and later. If you can't be sure that all your users have newer browsers than you can create your own function to format a number as a currency, as shown here.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Format a Number as Currency

The easiest and most proper way to format a number as a currency value is to use the Java Script Number object's toFixed method, as shown below.

var n = 314.7614;
var amt = n.toFixed(2);

The argument passed to the toFixed method sets the number of digits to the right of the decimal point. The toFixed method rounds the number up and adds zeros after the decimal point if needed to create the desired decimal length.

The toFixed method will only work in newer browsers like Internet Explorer version 5.5 and later. If you can't be sure that all your users have newer browsers than you can create your own function to format a number as a currency, as shown below.

function toCurrency(num)
{
  var n = Math.round(num * 100) / 100;

  var m = n.toString();
  var loc = m.indexOf(".");
  if(loc < 0)
  {
    loc = m.length;
    m += ".";
  }

  while(loc + 3 > m.length)
  {
    m += '0';
  }
  return m;
}

The first line in the toCurrency function moves first two digits of the number from the right of decimal to left of the decimal point (or adds zeros if required), rounds the number, then puts the decimal point back where it was.

The number is then converted to a string and the index of the decimal point in the string is acquired. The if structure checks if there is no decimal point in the number. if not, it puts one at the end of the number.

The while structure makes sure there are two digits to right of the decimal point. If not. it adds zeros as required. The function then returns the currency formatted number.

The line below shows how you could pass a number to the toCurrency function.

var k = 120.6234;
var amt = toCurrency(k);

Or just pass the numeric value as shown below.

var amt = toCurrency(120.6234);

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269