saravanak
saravanak

Reputation: 1

Javascript split integer and add decimal point

My integer value is 1210 and i want split this integer like 1 | 210 .Have to add decimal point on middle.

Eg:

var integer=1210;

Split this integer and add decimal value like this 1.210

Upvotes: 0

Views: 160

Answers (3)

JMP
JMP

Reputation: 4467

More generically you could use:

x=prompt('enter an integer');
xl=x.toString().length-1
alert((x/Math.pow(10,xl)).toFixed(xl));

(just make sure you enter an integer, preferably +ve, at the prompt)

Upvotes: 0

alexcavalli
alexcavalli

Reputation: 536

If you're always dealing with 4 digit numbers, dividing by 1000 will work (as mentioned in another answer) but you'll need to use toFixed to make sure javascript doesn't remove trailing zeros:

var x = 1210;
(x / 1000).toFixed(3) // => "1.210"
(x / 1000) + "" // => "1.21"

Upvotes: 0

void
void

Reputation: 36703

Why don't you just divide the number by 1000

var x = 1210;
var y = 1210/1000; //1.210 number
var z = y+"";      // 1.120 will be string here
console.log(y);    // Will output 1.210

Upvotes: 1

Related Questions