-4

I have the following function that automatically adds the commas to an American numerical expression which is some way of localisation. I need a minor change to this function.

if I have 160 * 54.08 = 8.652,8 as input to the function what can I do to show the output as 8.652,80 with two decimal places? Right now the function outputs as 8.652.8

function Numberformat(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? ',' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    return x1 + x2;
}
sabithpocker
  • 15,274
  • 1
  • 42
  • 75

3 Answers3

0

Check this answer:https://stackoverflow.com/a/149099/3025534

You can use:

  var profits=2489.8237
  profits.toFixed(3) //returns 2489.824 (round up)
  profits.toFixed(2) //returns 2489.82
  profits.toFixed(7) //returns 2489.8237000 (padding)

Then you can add the sign of '$'.

If you require ',' for thousand you can use:

Number.prototype.formatMoney = function(c, d, t){
var n = this, 
    c = isNaN(c = Math.abs(c)) ? 2 : c, 
    d = d == undefined ? "." : d, 
    t = t == undefined ? "," : t, 
    s = n < 0 ? "-" : "", 
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
    j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 };

And use it with:

(123456789.12345).formatMoney(2, '.', ',');

If you're always going to use '.' and ',', you can leave them off your method call, and the method will default them for you.

(123456789.12345).formatMoney(2);

If your culture has the two symbols flipped (i.e. Europeans), just paste over the following two lines in the formatMoney method:

d = d == undefined ? "," : d, 
t = t == undefined ? "." : t, 
Community
  • 1
  • 1
Alan
  • 2,046
  • 2
  • 20
  • 43
0

I would do something like this

var n = '1.000,00';

if (wrongFormat(n)) {
  n = n.replace(',', '#');
  n = n.replace('.', ',');
  n = n.replace('#', '.');
}

var val = 1*n;

//do something with val here
Dominique Fortin
  • 2,212
  • 15
  • 20
0

function Numberformat(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? ',' + x[1] : ',0';//if there is no decimal 
                                          //force ",00"
    x2 = x2.length < 3? x2 + "0" : x2;//if length of decimal + delimiter is 
                                      //less than 3 add an extra 0
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + '.' + '$2');
    }
    return x1 + x2;
}

document.body.innerHTML = Numberformat(160 * 54.08);//8.652,80
document.body.innerHTML += '<br>' + Numberformat(8560);//8.560,00
document.body.innerHTML += '<br>' + Numberformat(8560.0);//8.560,00
document.body.innerHTML += '<br>' + Numberformat(8560.1);//8.560,10
document.body.innerHTML += '<br>' + Numberformat(8560.01);//8.560,01
document.body.innerHTML += '<br>' + Numberformat(8560.009);//8.560,009
sabithpocker
  • 15,274
  • 1
  • 42
  • 75