Skip to content
Closed
Show file tree
Hide file tree
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
17 changes: 10 additions & 7 deletions src/Copper/Copper.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,13 @@ public static function scientific(): string

/**
* Format the number using SI units and prefixes.
* @param Unit $unit SI Unit to display using.
*
* @param Unit $unit SI Unit to display using.
*
* @parm bool $usePrefix Set whether to use prefixes.
* @param bool $useThrees Set whether to use only multiples of three in prefixes.
* @param int|null $precision Set the precision of the number.
*
* @param bool $useThrees Set whether to use only multiples of three in prefixes.
* @param int|null $precision Set the precision of the number.
* @return string Formatted number.
*/
public static function unit(Unit $unit, bool $usePrefix = true, bool $useThrees = true, ?int $precision = null): string
Expand All @@ -201,13 +204,13 @@ public static function unit(Unit $unit, bool $usePrefix = true, bool $useThrees
$value = self::$value;
$exponent = 0;

if($usePrefix) {
$exponent = (int) (floor(log10(abs($value))));
if ($usePrefix) {
$exponent = (int) floor(log10(abs($value)));

if ($useThrees || $exponent >= 3) {
$options = [
(int) floor($exponent / 3) * 3,
(int) ceil($exponent / 3) * 3
(int) ceil($exponent / 3) * 3,
];
$exponent =
abs($exponent - $options[0]) < abs($options[1] - $exponent)
Expand All @@ -218,7 +221,7 @@ public static function unit(Unit $unit, bool $usePrefix = true, bool $useThrees
$value /= (10 ** $exponent);
}

return self::$formatter->format($value) . ' ' . Prefix::from($exponent)->symbol() . $unit->value;
return self::$formatter->format($value).' '.Prefix::from($exponent)->symbol().$unit->value;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Copper/Enums/Prefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ enum Prefix: int
case RONTO = -27;
case QUECTO = -30;

public function symbol(): string {
return match($this) {
public function symbol(): string
{
return match ($this) {
self::QUETTA => 'Q',
self::RONNA => 'R',
self::YOTTA => 'Y',
Expand Down Expand Up @@ -62,7 +63,8 @@ public function symbol(): string {
};
}

public function multipleOfThree(): bool {
public function multipleOfThree(): bool
{
return ! abs(self::value) % 3;
}
}
6 changes: 3 additions & 3 deletions src/Copper/Enums/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ enum Unit: string
case SIEVERT = 'Sv';
case KATAL = 'kat';

public function name(): string {
return match($this)
{
public function name(): string
{
return match ($this) {
self::SECOND => 'Second',
self::METRE => 'Metre',
self::GRAM => 'Gram',
Expand Down
Loading