最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Format numbers as money, two decimals - Stack Overflow

programmeradmin4浏览0评论

How do I turn, say 1499, to 14.99? The raw numbers will always be listed as "1999", "2499", "9999" etc. with the 2 last numbers being decimals. Preferrably in jQuery.

The only code I've understood assumes that there are no decimals already, or wants to round them, similar to

var num = 10;
var result = num.toFixed(2);

The number 19999 = $199.99 The number 149 = $1.49 so two numbers from the back are decimals. Ideas? I've seen plugins that should work, but I'd prefer not having to use a plugin for this relatively small task.

How do I turn, say 1499, to 14.99? The raw numbers will always be listed as "1999", "2499", "9999" etc. with the 2 last numbers being decimals. Preferrably in jQuery.

The only code I've understood assumes that there are no decimals already, or wants to round them, similar to

var num = 10;
var result = num.toFixed(2);

The number 19999 = $199.99 The number 149 = $1.49 so two numbers from the back are decimals. Ideas? I've seen plugins that should work, but I'd prefer not having to use a plugin for this relatively small task.

Share Improve this question asked May 4, 2016 at 14:02 Algernop K.Algernop K. 4772 silver badges20 bronze badges 4
  • Can you share more test cases? or separating last two digits by ma is the only test case you are looking for? – gurvinder372 Commented May 4, 2016 at 14:04
  • 1 Take a look at numeraljs. – Bogdan Burym Commented May 4, 2016 at 14:04
  • How about change the number to String an replace the last two digits with '.xx'? – Reporter Commented May 4, 2016 at 14:07
  • OP do not want to turn 1499 to 1499.00, please read again the post. – vaso123 Commented May 4, 2016 at 14:08
Add a ment  | 

6 Answers 6

Reset to default 4

The code you have works perfectly, once you divide by 100...

function convert(value) {
    return (value / 100).toFixed(2);
}

console.log(convert(1499));
console.log(convert(2499));
console.log(convert(9999));

jsfiddle example

https://jsfiddle/8z15pb5c/

To avoid division which I do not trust due to floating point issues, here is one that does not use it:

function dec(num) {
  var str = String(num);
  if (str.length < 3) return "$0."+("0"+str).slice(-2);
  return "$"+str.substring(0,str.length-2)+"."+str.slice(-2);
}

document.write('<br/>'+dec(9))
document.write('<br/>'+dec(99))
document.write('<br/>'+dec(199))
document.write('<br/>'+dec(1999))
document.write('<br/>'+dec(19999))

I think a nice way to do this is to loop trough a string obtained from the number and just add a dot or whatever when it should be added.

function toCurrency(n) {
    var value = n.toString();
    var output = '$';
    for (var i = 0; i < value.length; ++i) {
        if (i === value.length - 2) output += '.';
        output += value[i];
    }
    return output;
}

console.log(toCurrency(2499)); // $24.99

This function could also be added as a prototype of Number like this:

Number.prototype.toCurrency = function() {
    var value = this.toString();
    var output = '$';
    for (var i = 0; i < value.length; ++i) {
        if (i === value.length - 2) output += '.';
        output += value[i];
    }
    return output;
}

var price = 12200;
price.toCurrency(); // "$122.00"

I would do it this way, but I must say that modifying primitives is not remended

A workaround would be divide your number by 100 before call toFixed. Sample: result = (num/100).toFixed(2)

function test(number){
    str = number.toString();
    var len = str.length;
    var x = '$' + str.substring(0, len-2) + "." + str.substring(len-2);    
    console.log(x);
}
function convert(num) {
    return '$' + (num / 100).toFixed(2);
}

var num1 = 2499;
var result1 = convert(num1); // $24.99

var num2 = 99;
var result2 = convert(num2); // $0.99

Demo

function convert(num) {
    return '$' + (num / 100).toFixed(2);
}

var num1 = 2499;
var result1 = convert(num1);

var num2 = 190;
var result2 = convert(num2);

var num3 = 99;
var result3 = convert(num3);

document.write(result1 + '</br>');
document.write(result2 + '</br>');
document.write(result3 + '</br>');

发布评论

评论列表(0)

  1. 暂无评论