Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/Toolbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down