Hi pals, if you divide a number and the answer has many number of decimal places, how can you declare specific# of decimal place | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi pals, if you divide a number and the answer has many number of decimal places, how can you declare specific# of decimal place

Example var x= 1 ; var y=3; var z=x/y; alert (z) ; Output is 0.333333333333 Me I want to declare an answer into 2 or 3 decimal places. Can anyone help me.

17th Nov 2017, 4:18 PM
Jafari Ibrahim
Jafari Ibrahim - avatar
3 Answers
+ 2
alert(z.toFixed(2));
17th Nov 2017, 7:49 PM
John Wells
John Wells - avatar
+ 1
Thanks a lot
18th Nov 2017, 1:52 AM
Jafari Ibrahim
Jafari Ibrahim - avatar
0
If you don't like the 6.00 type display, use: z = Math.trunc(z*100)/100;
18th Nov 2017, 1:57 AM
John Wells
John Wells - avatar