diff --git a/src/Toolbox.php b/src/Toolbox.php index eb0ae2d0..c300fe75 100644 --- a/src/Toolbox.php +++ b/src/Toolbox.php @@ -336,6 +336,38 @@ public static function getEnergy(float $p): string return sprintf(__('%1$s %2$s'), $p, $human_readable_unit); } + /** + * Format a volume in L + * + * @param float $p Volume in L + * + * @return string formatted volume + **/ + public static function getVolume(float $p): string + { + //TRANS: list of unit (Wh for watt.hour) + $units = [ + __('L', 'carbon'), + __('m³', 'carbon'), + __('da', 'carbon'), + __('h', 'carbon'), + __('K', 'carbon'), + ]; + + $multiple = 990; + foreach ($units as $human_readable_unit) { + if ($p < $multiple) { + break; + } + $p /= 1000; + } + + $p = self::dynamicRound($p); + + //TRANS: %1$s is a number maybe float or string and %2$s the unit + return sprintf(__('%1$s %2$s'), $p, $human_readable_unit); + } + public static function dynamicRound(float $number): float { if ($number < 10) { @@ -370,7 +402,7 @@ public static function getHumanReadableValue(float $value, array $unit): string return self::getEnergy($value) . $unit[1]; case 'm³': // Value is in m^3 - return sprintf(__('%1$s %2$s', 'carbon'), $value * 1000, 'L'); + return self::getVolume($value * 1000) . $unit[1]; case 'mol': break; }