Skip to content

Commit 0211c74

Browse files
authored
Merge pull request #9 from pwweb/fix/order-of-set-style-and-precision-matters
🐞 Fix order of style before precision
2 parents dbe021d + 53d0221 commit 0211c74

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/Copper/Copper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ public static function create(float|string|null $value = null, ?int $style = nul
8989
*/
9090
public static function decimal(?int $precision = null): string|bool
9191
{
92-
if (false === is_null($precision)) {
93-
self::$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
94-
}
9592
if (NumberFormatter::DECIMAL !== self::$style) {
9693
self::setStyle(NumberFormatter::DECIMAL);
9794
}
95+
if (false === is_null($precision)) {
96+
self::$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
97+
}
9898

9999
return self::$formatter->format(self::$value);
100100
}
@@ -184,12 +184,12 @@ public static function scientific(): string|bool
184184
*/
185185
public static function unit(Unit $unit, bool $usePrefix = true, bool $useThrees = true, ?int $precision = null): string
186186
{
187-
if (false === is_null($precision)) {
188-
self::$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
189-
}
190187
if (NumberFormatter::DECIMAL !== self::$style) {
191188
self::setStyle(NumberFormatter::DECIMAL);
192189
}
190+
if (false === is_null($precision)) {
191+
self::$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $precision);
192+
}
193193

194194
$value = self::$value;
195195
$exponent = 0;

tests/Unit/CopperTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
});
2727

2828
it('formats a number as decimal', function () {
29-
Copper::create(1234.56, locale: 'en-GB');
29+
Copper::create(1234.563, locale: 'en-GB');
30+
Copper::setStyle(2);
3031
$result = Copper::decimal(2);
3132
expect($result)->toBe('1,234.56');
3233
});
@@ -63,6 +64,7 @@
6364

6465
it('formats a number with SI units and prefixes', function (float $value, string $output) {
6566
Copper::create($value);
67+
Copper::setStyle(2);
6668
$result = Copper::unit(Unit::METRE);
6769
expect($result)->toBe($output);
6870
})->with([
@@ -87,6 +89,7 @@
8789

8890
it('formats a number with SI units and prefixes with specific precision', function () {
8991
Copper::create(123456);
92+
Copper::setStyle(2);
9093
$result = Copper::unit(Unit::METRE, precision: 2);
9194
expect($result)->toBe('0.12 Mm');
9295
});

0 commit comments

Comments
 (0)