Algernop K.
Algernop K.

Reputation: 477

Format numbers as money, two decimals

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.

Upvotes: 1

Views: 428

Answers (6)

isvforall
isvforall

Reputation: 8926

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>');

Upvotes: 2

Aramil Rey
Aramil Rey

Reputation: 3475

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 recommended

Upvotes: 2

Ronnie Smith
Ronnie Smith

Reputation: 18555

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

Upvotes: 2

mplungjan
mplungjan

Reputation: 177691

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))

Upvotes: 4

Ronan Lopes
Ronan Lopes

Reputation: 3398

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

Upvotes: 2

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

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.net/8z15pb5c/

Upvotes: 4

Related Questions