TL;DR: you may use this formula:
=SIGN(<NUMBER>)*((ABS(<NUMBER>)*10^<PRECISION>+2^52)-2^52)/10^<PRECISION>
where <NUMBER>
is your rounded number, and <PRECISION>
is number of decimals in the result. Of course, 2^52
is better replaced by its value, 4503599627370496
, to avoid repeated calculations.
Explanation, credits, and disclaimer:
LibreOffice uses double precision (64-bit) IEEE 754 numbers internally.
2^52
is the smallest double that cannot have a fractional part. Adding it to a positive number results in an integer, and the value of this integer depends on current rounding mode set in the program. By default, the rounding mode mandated by IEEE 754 is round-to-even, ~all currently used processors follow this, and LibreOffice does not change this mode. It means, that when you add 2^52
to absolute value of your number, the FPU will use this mode in the process of summation, and the result will be equal to "absolute value of your number rounded to even plus 2^52
". Now subtract 2^52
to obtain the rounded absolute value, apply the original sign, and you have the wanted rounded number. To have arbitrary precision, just multiply the number to appropriate power of ten before summation, and divide back after subtracting.
The method follows the code of LibreOffice’s rtl_math_round, which uses this smart technique for rtl_math_RoundingMode_HalfEven
case. The method is attributed to Ross Cottrell, “the original author who placed it into public domain”.
Note that there is some possibility, that in some cases, LibreOffice might run in an environment that sets a different rounding mode. In that case, the result of the formula will be wrong. I think that actual probability of such a situation is neglectable.