Jump to content

Formatting number to currency with the (commas etc)


elabuwa

Recommended Posts

Hi guys,

 

did some googling and the only to i came through was big as functions written separately to get a number formatted in the currency method.

You the know every three digits seperated with a comma and rounded upto two decimal points.

In php it is easy as value =  number_format($number,2).

I was wondering if there was a similar way in javascript.

 

thanks in advance

Link to comment
Share on other sites

Here's a simple function for you

 

<HTML> 
<HEAD>
<script> 
function formatCurrency(fieldObj)
{
    if (isNaN(fieldObj.value)) { return false; }
    fieldObj.value = '$ ' + parseFloat(fieldObj.value).toFixed(2);
    return true;
}
</script> 
</HEAD> 
<BODY> 

<input type="text" name="amount" onchange="formatCurrency(this);" />

</BODY>
</HTML>

Link to comment
Share on other sites

  • 2 months later...

I'm Posting Here regarding a way to format numbers like in PHP with number_format.  Here is my javascript verison i wrote yesterday.  Hope it helps you guys who are looking.  This is simply to add commons & mine is only for positive numbers.

function number_format(input_){
    if(input_>0){var input_string=input_.toString();var rebuild='';var rebuild_remaining=input_string;
var loop_='no';
for(i=0; rebuild_remaining.length>3; i++){loop_='yes';
rebuild = rebuild_remaining.substr((rebuild_remaining.length-3),3)+','+rebuild;
rebuild_remaining=rebuild_remaining.substr(0,(rebuild_remaining.length-3));
}if(loop_==='yes'){rebuild = rebuild_remaining+','+rebuild;
rebuild = rebuild.substr(0,(rebuild.length-1));}else{rebuild=input_string;}
return rebuild;}else{return 0;}}

 

Simple way to understand this code, you input a pure number, not a string.  the number is turned into a string inside this function so it can count the length of the string, you cant count the length of a number.  Secondly i installed a loop that will loop though our number string and slowly subtract 3 end digit numbers at a time and adding a , (comma) after each.  The string is being rebuild into  a new varible which is later edited with the correct formating and removing an extra comma from the end of the process.  So far i havent had 1 problem with this script and it works amazing with my other function that does the

 

var cost = price * amount;
var cost = number_format(cost);
document.getElementById('div_id').innerHTML=cost;
document.getElementById('input_id').value=cost;

 

Again i hope this helps

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.