1

I'm looking for method to achieve the following:

var exampleIntOne = 170;
var exampleIntTwo = 1700;
var exampleIntThree = 17000;
var exampleIntFour = 170000;

I would like to be able to convert the above to the following expected result:

1.70
17.00
170.00
1700.00

I have tried: exampleIntOne.toFixed(2); but this turns that example into: 170.00

BernardV
  • 515
  • 9
  • 22

3 Answers3

4

You can do this

function parseDecimal(numberVal){
   return (numberVal / 100).toFixed(2);
}

var exampleIntOne = 170 
console.log(parseDecimal(exampleIntOne));
var exampleIntTwo = 1700
console.log(parseDecimal(exampleIntTwo));
var exampleIntThree = 17000
console.log(parseDecimal(exampleIntThree));
var exampleIntFour = 170000
console.log(parseDecimal(exampleIntFour));

Where parseDecimal() is your common method to get the desired output format.

Ankit Agarwal
  • 29,658
  • 5
  • 35
  • 59
3
(yourNumber/100).toFixed(2)

That should do

Suresh Atta
  • 118,038
  • 37
  • 189
  • 297
1

function convertNum(num){
   var numStr=num.toString();   
   numStr=numStr.slice(0,-2);
   var newValue=numStr+'.'+'00';  
   return newValue;  
}
console.log(convertNum(17000));
liontass
  • 652
  • 7
  • 22