nicmart/Numbers

A way to get the nearest SI magnitude

Opened this issue · 0 comments

C-Duv commented

In both Number::getSuffixNotation() and MagnitudeSuffix::__toString(), the nearest SI magnitude is computed using:

3 * floor($magnitude/3)

It would be convenient if either Number or MagnitudeSuffix could provide such information.

In my case I am using PHP's round() with negative precision parameter to round a given number to k, M, G, etc. without any decimal part:

$suffixed = Number::n(
        round(
            $input_number,
            -1 * (3 * floor(Number::n($input_number)->getMagnitude()/3))
        )
    )
    ->getSuffixNotation();