1

how do I turn to be money string? 1200 should be 1,200

 function setdefaults() {
    document.getElementById('perpetual').checked = true
    document.getElementById('users').value = '1'; 
    math_perpetual = parseInt(document.getElementById('users').value) *   1200; 
    document.getElementById('result').innerHTML = "The total price is $" + math_perpetual;
}
  • http://stackoverflow.com/questions/3753483/javascript-thousand-separator-string-format – Mate Jul 01 '14 at 04:27
  • seems duplicate of http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript. have a look on it. – Mritunjay Jul 01 '14 at 04:27

5 Answers5

2

I think it would be easier to just use a library that could handle it for you. I use currencyFormatter.js - give it a try. Works on all browsers and pretty light-weight. It'll also add the currency symbols for you, and can format according to a specified locale:

OSREC.CurrencyFormatter.format(2534234, { currency: 'INR' }); // Returns ₹ 25,34,234.00
OSREC.CurrencyFormatter.format(2534234, { currency: 'EUR' }); // Returns 2.534.234,00 €
OSREC.CurrencyFormatter.format(2534234, { currency: 'EUR', locale: 'fr' }); // Returns 2 534 234,00 €

currencyFormatter.js on github

1

Take a look at the toLocaleString function in the number class:

Example from the MDN pages:

var number = 123456.789;

// request a currency format
alert(number.toLocaleString("de-DE", {style: "currency", currency: "EUR"}));
// → 123.456,79 €

// the Japanese yen doesn't use a minor unit
alert(number.toLocaleString("ja-JP", {style: "currency", currency: "JPY"}))
// → ¥123,457

// limit to three significant digits
alert(number.toLocaleString("en-IN", {maximumSignificantDigits: 3}));
// → 1,23,000 * doesn't work for chrome
Shreedhar
  • 5,502
  • 3
  • 22
  • 27
arielnmz
  • 8,354
  • 9
  • 38
  • 66
0

The below code will format your money in dollars:

function formatDollar(num) {
    var p = num.toFixed(2).split(".");
    return "$" + p[0].split("").reverse().reduce(function(acc, num, i, orig) {
        return  num + (i && !(i % 3) ? "," : "") + acc;
    }, "") + "." + p[1];
}
arielnmz
  • 8,354
  • 9
  • 38
  • 66
0
function addThousandsSeparators(n) {
  return (''+n).split('').reverse().join('')
      .match(/(\d{1,3})/g).join(',').split('')
      .reverse().join('');
}

addThousandsSeparators(123);        // =>           "123"
addThousandsSeparators(1234);       // =>         "1,234"
addThousandsSeparators(12345);      // =>        "12,345"
addThousandsSeparators(123456);     // =>       "123,456"
addThousandsSeparators(1234567);    // =>     "1,234,567"
addThousandsSeparators(12345678);   // =>    "12,345,678"
addThousandsSeparators(123456789);  // =>   "123,456,789"
addThousandsSeparators(1234567890); // => "1,234,567,890"
maerics
  • 151,642
  • 46
  • 269
  • 291
0

You can use Intl.NumberFormat. Here is an example:

const number = 123456.789;

console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// expected output: "123.456,79 €"

// the Japanese yen doesn't use a minor unit
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
// expected output: "¥123,457"

// limit to three significant digits
console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number));
// expected output: "1,23,000"

Demo

Khabir
  • 5,370
  • 1
  • 21
  • 33