MathUtils   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 8
c 1
b 1
f 0
dl 0
loc 24
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A smartRound() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rico\Slib;
6
7
abstract class MathUtils
8
{
9
    /**
10
     * Rounds a $number adding decimal part only when int part of $number < $idealLength.
11
     *
12
     * @param float $number
13
     * @param int   $idealLength
14
     *
15
     * @return float
16
     */
17
    public static function smartRound(float $number, int $idealLength = 3): float
18
    {
19
        if ($number == 0) {
20
            return 0.0;
21
        }
22
23
        $intPart = intval($number);
24
25
        $precision = $idealLength - mb_strlen((string) $intPart);
26
        if ($precision < 0) {
27
            $precision = 0;
28
        }
29
30
        return (float) sprintf("%.{$precision}f", $number);
31
    }
32
}
33