I have two float numbers in Javascript with high and lower precision (not known in advance) for example:
1545.165
12.15364613
How can I round the high precision number to the same precision as lower precision number? In our case to
12.154
3 Answers 3
You can wrangle around with the toFixed()
function.
Syntax: <number>.toFixed(precision)
For example:
12.15364613.toFixed(3)
gives 12.154
But to make it work in your case you want to do something like this:
Warning untested code, I only meant to give you a general idea of how this can be done, of course the code below can be improved.
var num1 = 12.15364613;
var num1Str = (12.15364613).toString();
var num1Precision = parseInt(num1Str.substring(num1Str.indexOf('.')).length - 1);
var num2 = 12.154;
var num2Str = (12.154).toString();
var num2Precision = parseInt(num2Str.substring(num2Str.indexOf('.')).length - 1);
if(num1Precision > num2Precision){
num1 = num1.toFixed(num2Precision);
} else{
num2 = num2.toFixed(num1Precision);
}
6 Comments
Pure Math Solution
In order to get precision of unknown number (variable) you can use such a function:
function getPrecision(x) {
// i < 10 - just to avoid infinite looping
// (which may occur if x is a real number like PI)
// x - Math.floor(x) + (x < 0? 1 : 0) is a fractional part of x
for (var i = 0; i < 10 && x - Math.floor(x) + (x < 0? 1 : 0) > 0; i++, x *= 10) ;
return i;
}
So the code should like this:
// array of given numbers:
aVals = [1545.165, 12.15364613];
// array of numbers' precisions:
var aPre = aVals.map(getPrecision);
// find the least precision:
var minPre = Math.min.apply(null, aPre);
// result array of fixed numbers with the same precision:
var aValsFixed = aVals.map(function(v) { return v.toFixed(minPre); });
Comments
You mean something like this?
var number = 1.2333445454;
var n = number.toFixed(3);
alert(n);