0

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
asked Mar 17, 2015 at 14:34

3 Answers 3

2

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);
}
answered Mar 17, 2015 at 14:36

6 Comments

How do you know that precision is equal to 3 ?
@ФилиппЦветков You choose what precision you find useful.
@Kroltan I think OP means something else with precision, but I also don't know what.
The precision is not known in advance. I put the precise numbers as examples
Better. But there is no any native number function to get precision without converting it to string?
|
1

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); });
answered Mar 17, 2015 at 16:12

Comments

0

You mean something like this?

var number = 1.2333445454;
var n = number.toFixed(3);
alert(n);
answered Mar 17, 2015 at 14:40

2 Comments

Why you fixed precision to 3?
The precision is not known in advance. I put the precise numbers as examples

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.