I have a number which looks number this:
JavaScript
x
2
1
800.60000305176541
2
This number changes all the time.
So I’m doing this:
JavaScript
1
4
1
var mynumber = 800.60000305176541
2
3
var changenumber = mynumber.toFixed(3);
4
This is displaying 800.600
… I need it to display the last 3 like:
JavaScript
1
2
1
800.541
2
How can I do this?
Advertisement
Answer
You can convert to string and do your manipulations. Please note we are loosing the right most digit due to limits of javascript.
JavaScript
1
10
10
1
var num = 800.60000305176541;
2
3
var str = "" + num
4
var arr = str.split(".");
5
var result = arr[0]
6
if (arr[1]) {
7
result += "." + arr[1].slice(-3)
8
}
9
console.log(num)
10
console.log(result)