Excel ROUND function implementation
How would you implement the ROUND function:
ROUND(value, number of digits)
pi=3.14159265358979323
so, for example, ROUND(pi, 3) = 3.142
if you have these functions at your disposal:
AINT - truncates the value by an integer
ANINT - calculates the closest integer
NINT - returns the nearest integer to the argument
or ignoring the above functions, how does floating ROUND work at all?
0
a source to share
2 answers
I would guess excuse my pseudocode
function Round(value, num){
numsToSave = POWER(10, num);
value *= numsToSave ; //Get the numbers we don't want rounded on the left side of the floating point
value = AINT( ANINT(value) );
value /= numsToSave;
return value;
}
or
function Round(value, num){
numsToSave = POWER(10, num);
value *= numsToSave ; //Get the numbers we don't want rounded on the left side of the floating point
value = NINT(value);
value /= numsToSave;
return value;
}
+1
a source to share