36

I've got this number as a integer 439980

and I'd like to place a decimal place in 2 places from the right. to make it 4399.80

the number of characters can change any time, so i always need it to be 2 decimal places from the right.

how would I go about this?

thanks

3 Answers 3

53
function insertDecimal(num) {
   return (num / 100).toFixed(2);
}
Sign up to request clarification or add additional context in comments.

2 Comments

this method works for most numbers, however try doing this for a nuber like 10, or 7, 10, 20, 60, 90 and you will see it break.
@Ayyoub - It works for me. Input 10 and it returns "0.10", and for 7 it returns "0.07" - what do you think it should return for those cases?
6

Just adding that toFixed() will return a string value, so if you need an integer it will require 1 more filter. You can actually just wrap the return value from nnnnnn's function with Number() to get an integer back:

function insertDecimal(num) {
   return Number((num / 100).toFixed(2));
}

insertDecimal(99552) //995.52
insertDecimal("501") //5.01

The only issue here is that JS will remove trailing '0's, so 439980 will return 4399.8, rather than 4399.80 as you might hope:

insertDecimal(500); //5

If you're just printing the results then nnnnnn's original version works perfectly!

notes

JavaScript's Number function can result in some very unexpected return values for certain inputs. You can forgo the call to Number and coerce the string value to an integer by using unary operators

return +(num / 100).toFixed(2);

or multiplying by 1 e.g.

return (num / 100).toFixed(2) * 1;

TIL: JavaScript's core math system is kind of weird

2 Comments

That's right, JavaScript's core math system is can of weird. You could get things like this: Number(1.11) * 100 = 111.00000000000001. Fun.
Regarding your note about the unary plus operator versus Number(), you seem to be saying that they can give different results? What are some example values where the results would be different? (Personally I prefer the unary plus, but I thought the results were the same.)
1

Another Method

  function makeDecimal(num){

    var leftDecimal = num.toString().replace('.', ''),
        rightDecimal = '00';
    
    if(leftDecimal.length > 2){          
      rightDecimal = leftDecimal.slice(-2);
      leftDecimal = leftDecimal.slice(0, -2);
    }
    
    var n = Number(leftDecimal+'.'+rightDecimal).toFixed(2);        
    return (n === "NaN") ? num:n        
  }

 makeDecimal(3) // 3.00

 makeDecimal(32) // 32.00

 makeDecimal(334) // 3.34

 makeDecimal(13e+1) // 1.30

Or

    function addDecimal(num){
        var n = num.toString();
        var n = n.split('.');
        if(n[1] == undefined){
            n[1] = '00';
        }
        if(n[1].length == 1){
            n[1] = n[1]+'0';
        }
        return n[0]+'.'+n[1];
    }

addDecimal(1); // 1.00

addDecimal(11); // 11.00 

addDecimal(111); // 111.00

Convert Numbers to money.

    function makeMoney(n){
        var num = n.toString().replace(/\$|\,/g,'');
        if(isNaN(num))
            num = "0";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num*100+0.50000000001);
        cents = num%100;
        num = Math.floor(num/100).toString();
        if(cents<10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
        return (((sign)?'':'-') + '$' + num + '.' + cents);

    }   

One More.

    function addDecimal(n){
      return parseFloat(Math.round(n * 100) / 100).toFixed(2);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.