Solved

How to format number

Posted on 2002-04-26
3
569 Views
Last Modified: 2008-02-01
How do you format a javascript number as 9,999,990.00 as in java
0
Comment
Question by:EdwardSee
3 Comments
 
LVL 1

Accepted Solution

by:
rajeev1972 earned 50 total points
Comment Utility
Use this sample

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function addComma(numVar)
{
     var formattedNumber="";
     var commaNumber = "";
     var j = numVar.indexOf(".");

     if (j != -1)
     {
          for(i = numVar.length; i>j; i--)
          {    
               formattedNumber = formattedNumber + numVar.charAt(i-1);
          }
     }
     else
          j = numVar.length;

     for (i = j; i > 0; i--)
     {
          if ( (i != j) && ((j - i) % 3 == 0))
                 formattedNumber = formattedNumber + ",";
          formattedNumber = formattedNumber + numVar.charAt(i-1);
     }
     for (i = formattedNumber.length - 1; i >= 0; i--)
          commaNumber = commaNumber + formattedNumber.charAt(i);
     return commaNumber;
}

//-->
</SCRIPT>
</HEAD>

<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
alert(addComma("12312321.9800"));
//-->
</SCRIPT>
</BODY>
</HTML>
0
 
LVL 12

Expert Comment

by:ahosang
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

points to rajeev1972
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
ahosang
EE Cleanup Volunteer
0
 

Expert Comment

by:modulo
Comment Utility
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now