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


If you don't need to worry about overflow, here's how:

ROUND(value, nod) = NINT(value * POWER(10, nod)) / POWER(10, nod)

      



Otherwise, you need to take care of the integer part and the float part separately.

+4


a source


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







All Articles