Main Topics
Browse All TopicsHi
I am trying to format a given number(eg. 12345) to get the output as 1,234.56
the below function is fine for formatting a number to xxx.xx
function fmtPrice(value)
{
result="$"+Math.floor(valu
var cents=100*(value-Math.floo
result += Math.floor(cents / 10);
result += Math.floor(cents % 10);
return result;
}
I am trying to get a format if the number is in thousands as x,xxx.xx
(If i pass xxxxxx to the above function i am getting only xxxx.xx)
thanks for your help
regards
vihar123
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
<script>
function formatNumber( num , firstDelimiter , secondDelimiter){
var tempNum = num.toString();
var newNum = '';
newNum = secondDelimiter + tempNum.substring( tempNum.length-2);
tempNum = tempNum.substring( 0, tempNum.length-2);
while (tempNum.length > 3){
newNum = firstDelimiter + tempNum.substring( tempNum.length-3) + newNum;
tempNum = tempNum.substring( 0, tempNum.length-3);
}
newNum ='$' + tempNum + newNum;
return newNum
}
formatNumber(1000000000000
formatNumber(100000, '.' , ',');
formatNumber(100000, '=' , '-');
</script>
http://javascript.internet
Here is the example
<!-- TWO STEPS TO INSTALL CURRENCY FORMAT:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Cyanide_7 (leo7278@hotmail.com) -->
<!-- Web Site: http://www7.ewebcity.com/c
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet
<!-- Begin
function formatCurrency(num) {
num = num.toString().replace(/\$
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000
cents = num%100;
num = Math.floor(num/100).toStri
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+
num = num.substring(0,num.length
num.substring(num.length-(
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<center>
<form name=currencyform>
Enter a number then click the button: <input type=text name=input size=10 value="1000434.23">
<input type=button value="Convert" onclick="this.form.input.v
<br><br>
or enter a number and click another field: <input type=text name=input2 size=10 value="1000434.23" onBlur="this.value=formatC
</form>
</center>
<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsou
</center><p>
<!-- Script Size: 1.47 KB -->
Here my version:
<script>
function fmtPrice(theVal){
resVal=Math.round(theVal*1
if(resVal<100){
resVal = "0."+resVal;
} else {
resVal += "";
resVal = resVal.substr(0,resVal.len
for(i=resVal.length-6;i>0;
resVal = resVal.substr(0,i)+","+res
}
}
return "$"+resVal;
}
alert(fmtPrice(10000000000
alert(fmtPrice(10000000));
alert(fmtPrice(100.06));
</script>
Hi friends,
i dont know why its not executing in my files.
As you are experts,is it possible to add functionality to this function only to get formatted for more than 4 digits?
as i am beginner,i cant understand your code.
If possible please enhance this code to x,xxx.xx format.
function fmtPrice(value)
{
result="$"+Math.floor(valu
var cents=100*(value-Math.floo
result += Math.floor(cents / 10);
result += Math.floor(cents % 10);
return result;
}
this function works fine for xxx.xx only
i want x,xxx.xx format.
I am not saying that your code is not right.As i am beginner in javascript,i cant understand your code.
please only enhances the above function (if possible).
thanks
vihar123
Sorry, typo.
Take this:
<script>
function fmtPrice(theVal){
resVal=Math.round(theVal*1
if(resVal<100){
resVal = resVal/100;
} else {
resVal += "";
resVal = resVal.substr(0,resVal.len
for(i=resVal.length-6;i>0;
resVal = resVal.substr(0,i)+","+res
}
}
return "$"+resVal;
}
alert(fmtPrice(10000000000
alert(fmtPrice(10000000));
alert(fmtPrice(100.06));
alert(fmtPrice(0.05));
</script>
Hi jaysolomon,
i tried your example.
but not working for me.i dont know why.
it would be great if you enhance functionality of my function to format 4 digit numbers as x,xxx,xx
function fmtPrice(value)
{
result="$"+Math.floor(valu
var cents=100*(value-Math.floo
result += Math.floor(cents / 10);
result += Math.floor(cents % 10);
return result;
}
vihar123
here it is working version again copy and paste it
<html>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Cyanide_7 (leo7278@hotmail.com) -->
<!-- Web Site: http://www7.ewebcity.com/c
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet
<!-- Begin
function formatCurrency(num) {
num = num.toString().replace(/\$
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000
cents = num%100;
num = Math.floor(num/100).toStri
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+
num = num.substring(0,num.length
num.substring(num.length-(
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
// End -->
</script>
</HEAD>
<BODY>
<center>
<form name=currencyform>
Enter a number then click the button: <input type=text name=input size=10 value="1000434.23">
<input type=button value="Convert" onclick="this.form.input.v
<br><br>
or enter a number and click another field: <input type=text name=input2 size=10 value="1000434.23" onBlur="this.value=formatC
</form>
</center>
<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsou
</center><p>
</body>
</html>
Cleanup Volunteer
im willing to do it as long as i dont have to ask questions doing it.
i did a bet on how long can i stay without asking any question.
so far im able to do it since all theproblems i had i was able to figure out myself
or find it it over the net.
if there is a way to do it without asking Q im more then willing to do so.
Nushi.
Business Accounts
Answer for Membership
by: NushiPosted on 2004-01-07 at 11:20:09ID: 10064652
vihar123
i gave you an answer in your prevoiur question earlier today.
Nushi.