Reputation: 320
Eg. if I give 150 //output should be 15.0 If 1565 //output should be 156.5
Can anyone provide best code for the above logic?
Upvotes: 0
Views: 706
Reputation: 12152
Use .parseFloat()
and .toFixed(1)
to set precision of one. The values provided in the toFixed() will set the precision till that value
var a = 150;
console.log(parseFloat(a / 10).toFixed(1))
a = 1565;
console.log(parseFloat(parseFloat(a / 10).toFixed(1))) //to get the output type as number
Upvotes: 4