Ejilarasan J
Ejilarasan J

Reputation: 320

JQuery - Change integer to float with one decimal point

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

Answers (1)

ellipsis
ellipsis

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

Related Questions