How to work with decimal places after the point in JavaScript

8 Answers

0 votes
var n = 3.457;

document.write(n.toFixed(0));
 
/*
run:

3 
 
*/

 



answered Jun 11, 2015 by avibootz
0 votes
var n = 3.567;

document.write(n.toFixed(0));
 
/*
run:

4 
 
*/

 



answered Jun 11, 2015 by avibootz
0 votes
var n = 3.557;

document.write(n.toFixed(0));
 
/*
run:

4 
 
*/

 



answered Jun 11, 2015 by avibootz
0 votes
var n = 3.569;

document.write(n.toFixed(1));
 
/*
run:

3.6 
 
*/

 



answered Jun 11, 2015 by avibootz
0 votes
var n = 3.569;

document.write(n.toFixed(2));
 
/*
run:

3.57 
 
*/

 



answered Jun 11, 2015 by avibootz
0 votes
var n = 3.569;

document.write(n.toFixed(3));
 
/*
run:

3.569  
 
*/

 



answered Jun 11, 2015 by avibootz
0 votes
var n = 3.569;

document.write(n.toFixed(4));
 
/*
run:

3.5690 
 
*/

 



answered Jun 11, 2015 by avibootz
0 votes
var n = 3.569;

document.write(n.toFixed(5));
 
/*
run:

3.56900
 
*/

 



answered Jun 11, 2015 by avibootz
...