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

9
  • 5
    So... divide it by 100...? Commented Jul 4, 2017 at 9:34
  • @RoryMcCrossan Just /100 wont give desired result. Have to do .toFixed(2) too. Commented Jul 4, 2017 at 9:38
  • Possible duplicate of Round to at most 2 decimal places (only if necessary) Commented Jul 4, 2017 at 9:39
  • Could you please respond to the answer so that i / (we) can know it is working for you or if you need any changes let know. Commented Jul 4, 2017 at 9:43
  • @BernardY can you please comment if it worked for you? And if yes, i would request you to mark it green. Commented Jul 4, 2017 at 10:08

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think you need parseFloat here.
3
(yourNumber/100).toFixed(2)

That should do

Comments

1

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

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.