adds commas seperator in javascript

window.document.getElementById('<%=product_Price.ClientID %>').value = "$" +(parseFloat(t5) * parseFloat(t1)).toFixed(2) ;
How can I display the value returned from above as $8,000.45
Currently it returns the value as $8000.45

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I've always used a function called FloatToDollarString, I'll post that up right now, it basically is a loop that adds a comma every 3 spaces after the decimal until the end of the string.
function FloatToDollarString(flt) {
	// round off to nearest dollar
	var str = "" + Math.round(flt)
	// add commas
	pos = str.length;  // str.indexOf(".");
	pos -= 4;
	while(pos >= 0) {
		str = str.substring(0,pos+1) + "," + str.substring(pos+1, str.length);
		pos -= 3;
	return str;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PNKJAuthor Commented:
Thanks but I cannot figure out how to add this to the following.
window.document.getElementById('<%=product_Price.ClientID %>').value = "$" +(parseFloat(t5) * parseFloat(t1)).toFixed(2) ;
Michel PlungjanIT ExpertCommented:
window.document.getElementById('<%=product_Price.ClientID %>').value = "$" +FloatToDollarString((parseFloat(t5) * parseFloat(t1)).toFixed(2)) ;
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

PNKJAuthor Commented:
thanks a lot  mplungjan but i found the solution and it works fine. i tried to use ur solution but it expects an object
function addCommas(val) {
    var regEx = /(\d+)(\d{3})/;
    while (regEx.test(val)) {
        val = val.replace(regEx, '$1' + ',' + '$2');
    return val;

window.document.getElementById('<%=product_Price.ClientID %>').value = "$" +addCommas((parseFloat(t5) * parseFloat(t1)).toFixed(2)) ;

How can i close this question?
Michel PlungjanIT ExpertCommented:
You can click the delete question.
I have a comment though: You post a solution which is identical to mine except you used another script than the one suggested by Ryan.

The suggestion I made of course assumes you ALSO added the function FloatToDollarString to your page

If you look at the call

FloatToDollarString((parseFloat(t5) * parseFloat(t1)).toFixed(2))

you will see it is exactly the same as

addCommas((parseFloat(t5) * parseFloat(t1)).toFixed(2))

just a different function is called - your addComma is a better script since it does not cut the decimals but that is not relevant to my previous comment.

PNKJAuthor Commented:
mplungjan:  I did try your FloatToDollarString((parseFloat(t5) * parseFloat(t1)).toFixed(2))
but it didnot work before i added addCommas((parseFloat(t5) * parseFloat(t1)).toFixed(2))
could be some other reason that FloatToDollarString didnot work for me. I think it was expecting the object that is the error I was getting.
Michel PlungjanIT ExpertCommented:
Yes it of course expected the
function FloatToDollarString () {

to be somewhere inside the script  tags
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.