2

In my database, I store money values without any points or commas. So I need a Javascript function that converts it into money string.

Example: Convert 500 (five dollars) into 5.00 , 100 into 1.00, 550 into 5.50 etc

Can anybody post a function like that?

user1091856
  • 3,032
  • 6
  • 31
  • 42
  • do you mean you just want to divide it by 100? Or do you need localization as well? – JohnP Mar 10 '12 at 08:03
  • 1
    Please post what code you have so far – Chetter Hummin Mar 10 '12 at 08:05
  • I have no code so far. This is what I want: var price = convertFunction(510); //price is "5.10" If I simply divide by 100, 510 will return 5.1 – user1091856 Mar 10 '12 at 08:09
  • possible duplicate of [How can I format numbers as money in JavaScript?](http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript) – Didier Ghys Mar 10 '12 at 08:10
  • Microsoft also made a [jquery globalization plugin](http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx) – Didier Ghys Mar 10 '12 at 08:11

4 Answers4

2

You can use something like:

function toAmount(amount){
  amount = Number(amount);
  if (isNaN(amount)){
    throw 'invalid: can\'t convert input';
  }
  return (amount/100).toFixed(2);
}
//usage
toAmount(500);    //=> 5.00
toAmount(520873); //=> 5208.73
toAmount('500');  //=> 5.00

Or use that function as an extension to Number

Number.prototype.toAmount = function toAmount(amount){
  return (this/100).toFixed(2);
};
//usage:
(500).toAmount();    //= 5.00
(520873).toAmount(); //=> 5208.73
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • Why not use `.toFixed()` rather than `toPrecision()` as `toFixed()` deals with the exact number of points after the decimal point which is what you really want (also shown in my answer)? This way may work, but doesn't seem as direct. – jfriend00 Mar 10 '12 at 08:13
  • @jfriend: saturday morning blindness perhaps. You're right, I'll edit the answer. – KooiInc Mar 10 '12 at 08:18
0

if it easy just divide your value on 100; after this you can add ".00" value if unnecessary

​var valdb = 500;
var format = (valdb/100)+".00";
alert(format);

Arbejdsglæde
  • 13,670
  • 26
  • 78
  • 144
0

You can use various string operations (I chose to use slice()) to extract the last two characters, insert a period in between and construct the final string. The advantage of doing it this way is you avoid some of the inaccuracies that floating point math can sometimes have:

var num = 500;
var numStr = num.toString();
var formattedStr = numStr.slice(0,-2) + "." + numStr.slice(-2);

If you just wanted to use floating point math, you could do:

var num = 500;
var formattedStr = (num / 100).toFixed(2).toString();

The operative part of this last one is the toFixed() method that rounds and zero pads a decimal number to an exact number of digits (which is exactly what you want for money). You can read about toFixed() in this MDN reference.

You can see both of them working here: http://jsfiddle.net/jfriend00/pApVR/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Simply divide the number by 100 then to make sure it always has decimals use the Javascript function toFixed(2).

So it looks like:

function ConvertCentsToDollars(cents) {
    var dollars = cents / 100;
    var dollars = dollars.toFixed(2);
    return dollars;
}
Tim
  • 7,660
  • 3
  • 30
  • 33