ndhai
asked on
round un number
Hello
here is my code to round un number
/************************* ********** ********** ********** */
// Round : round(8.56475, 2) => 8.56
/************************* ********** ********** ********** */
function roundFloat( value, n_precision ) {
var tem = 1.*Math.pow(10, n_precision);
var retval = Math.round(1.*value*tem)/t em;
return retval;
}
It works well but I have one problem following
roundFloat( 256.905, 2 ) = 256.9 (false) instead 256.91 (true)
I dit verify and I see the reason : Javascript calculs "1.*256.905*100 = 25690.499999999996" (false) instead 25690.5
I dont know how I can correct it ?
thanks a lot
best regards
Hai
here is my code to round un number
/*************************
// Round : round(8.56475, 2) => 8.56
/*************************
function roundFloat( value, n_precision ) {
var tem = 1.*Math.pow(10, n_precision);
var retval = Math.round(1.*value*tem)/t
return retval;
}
It works well but I have one problem following
roundFloat( 256.905, 2 ) = 256.9 (false) instead 256.91 (true)
I dit verify and I see the reason : Javascript calculs "1.*256.905*100 = 25690.499999999996" (false) instead 25690.5
I dont know how I can correct it ?
thanks a lot
best regards
Hai
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hello Rakafkaven
thanks but It doestn't work :((
when I tried alert( roundFloat(256.905, 2) );, it showed 256.910000000149
Hai
thanks but It doestn't work :((
when I tried alert( roundFloat(256.905, 2) );, it showed 256.910000000149
Hai
ASKER
>> alert( 256.905.toFixed(2) )
excellent response
thanks a lot
Hai
excellent response
thanks a lot
Hai
Not only do I waste power, the method I used doesn't work, since it the floating point operations still occur on the item. Rather than trying to avoid the operations, kicking them into an integer with a second Math.round and an additional decimal shifting operation works consistently. Of course, it wastes even MORE power, but it does return the answer without the potential for padding that toFixed has.
function roundFloat( value, n_precision ) {
value = value;
var tem = 1.*Math.pow(10, n_precision);
var retval = Math.round(Math.round(valu e*tem*100) /100)/tem;
return retval;
}
function roundFloat( value, n_precision ) {
value = value;
var tem = 1.*Math.pow(10, n_precision);
var retval = Math.round(Math.round(valu
return retval;
}
If you want to strip the zeros, then check this:
alert( roundFloat(256.905,2) )
function roundFloat( value, n_precision ) {
return value.toFixed(n_precision) *1;
}
alert( roundFloat(256.905,2) )
function roundFloat( value, n_precision ) {
return value.toFixed(n_precision)
}
If I didn't know better, I'd think that you knew what you were doing. :)
OK, there is nothing stripped in above number, but you know what I mean :)
function roundFloat( value, n_precision ) {
value = value + 50000;
var tem = 1.*Math.pow(10, n_precision);
var retval = (Math.round(1.*value*tem)/
return retval;
}