diff --git a/src/Copper/Copper.php b/src/Copper/Copper.php index cbc26f8..cda9cb1 100755 --- a/src/Copper/Copper.php +++ b/src/Copper/Copper.php @@ -89,12 +89,12 @@ public static function create(float|string|null $value = null, ?int $style = nul */ public static function decimal(?int $precision = null): string|bool { - if (false === is_null($precision)) { - self::$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); - } if (NumberFormatter::DECIMAL !== self::$style) { self::setStyle(NumberFormatter::DECIMAL); } + if (false === is_null($precision)) { + self::$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); + } return self::$formatter->format(self::$value); } @@ -184,12 +184,12 @@ public static function scientific(): string|bool */ public static function unit(Unit $unit, bool $usePrefix = true, bool $useThrees = true, ?int $precision = null): string { - if (false === is_null($precision)) { - self::$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); - } if (NumberFormatter::DECIMAL !== self::$style) { self::setStyle(NumberFormatter::DECIMAL); } + if (false === is_null($precision)) { + self::$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision); + } $value = self::$value; $exponent = 0; diff --git a/tests/Unit/CopperTest.php b/tests/Unit/CopperTest.php index cc3bbc5..c460a47 100644 --- a/tests/Unit/CopperTest.php +++ b/tests/Unit/CopperTest.php @@ -26,7 +26,8 @@ }); it('formats a number as decimal', function () { - Copper::create(1234.56, locale: 'en-GB'); + Copper::create(1234.563, locale: 'en-GB'); + Copper::setStyle(2); $result = Copper::decimal(2); expect($result)->toBe('1,234.56'); }); @@ -63,6 +64,7 @@ it('formats a number with SI units and prefixes', function (float $value, string $output) { Copper::create($value); + Copper::setStyle(2); $result = Copper::unit(Unit::METRE); expect($result)->toBe($output); })->with([ @@ -87,6 +89,7 @@ it('formats a number with SI units and prefixes with specific precision', function () { Copper::create(123456); + Copper::setStyle(2); $result = Copper::unit(Unit::METRE, precision: 2); expect($result)->toBe('0.12 Mm'); });