From edfbacd6006a418d5f96758eb75cfc1e5f433388 Mon Sep 17 00:00:00 2001 From: Richard Browne Date: Wed, 19 Feb 2025 20:39:59 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9E=20Fix=20order=20of=20style=20b?= =?UTF-8?q?efore=20precision?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Copper/Copper.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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; From 53d0221ed810e06154fddd81243a40cad984f23f Mon Sep 17 00:00:00 2001 From: Richard Browne Date: Wed, 19 Feb 2025 20:45:41 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A7=AA=20Update=20tests=20to=20check?= =?UTF-8?q?=20the=20precision=20still=20works=20when=20the=20style=20chang?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Unit/CopperTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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'); });