Skip to content

Commit 3f92ce1

Browse files
authored
Fix Missing Cents to Dollar Conversion (#131)
1 parent 9b28a41 commit 3f92ce1

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

src/Objects/Geography/Currency.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use EncoreDigitalGroup\StdLib\Objects\Support\Traits\HasEnumValue;
77

88
/**
9+
* @method static string format(float|int $amount, string $symbol = "$", int $precision = 2, string $decimals = '.', string $thousands = ',')
910
* @method string format(float|int $amount, int $precision = 2)
10-
* @method static string format(float|int $amount, string $symbol, int $precision = 2, string $decimals = '.', string $thousands = ',')
1111
*/
1212
enum Currency: string
1313
{
@@ -176,7 +176,7 @@ enum Currency: string
176176

177177
private static function formatGeneric(int $amount, string $symbol = "$", int $precision = 2, string $decimals = ".", string $thousands = ","): string
178178
{
179-
$formattedAmount = number_format($amount, $precision, $decimals, $thousands);
179+
$formattedAmount = number_format($amount / 100, $precision, $decimals, $thousands);
180180

181181
return "{$symbol}{$formattedAmount}";
182182
}
@@ -485,7 +485,7 @@ public function local(): string
485485
private function formatCurrency(int $amount, int $precision = 2): string
486486
{
487487
$symbol = $this->symbol();
488-
$formattedAmount = number_format($amount, $precision);
488+
$formattedAmount = number_format($amount / 100, $precision);
489489

490490
return "{$symbol}{$formattedAmount}";
491491
}

tests/Unit/CurrencyTest.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,60 +28,60 @@
2828

2929
describe('Currency instance format method', function () {
3030
test('formats basic amounts with default precision', function () {
31-
expect(Currency::UnitedStates->format(1000))->toBe('$1,000.00')
32-
->and(Currency::Eurozone->format(1000))->toBe('1,000.00')
33-
->and(Currency::Japan->format(1000))->toBe('¥1,000.00')
34-
->and(Currency::UnitedKingdom->format(1000))->toBe('£1,000.00');
31+
expect(Currency::UnitedStates->format(1000))->toBe('$10.00')
32+
->and(Currency::Eurozone->format(1000))->toBe('10.00')
33+
->and(Currency::Japan->format(1000))->toBe('¥10.00')
34+
->and(Currency::UnitedKingdom->format(1000))->toBe('£10.00');
3535
});
3636

3737
test('formats amounts with custom precision', function () {
38-
expect(Currency::UnitedStates->format(1000, 0))->toBe('$1,000')
39-
->and(Currency::UnitedStates->format(1000, 3))->toBe('$1,000.000')
40-
->and(Currency::Japan->format(1000, 0))->toBe('¥1,000');
38+
expect(Currency::UnitedStates->format(1000, 0))->toBe('$10')
39+
->and(Currency::UnitedStates->format(1000, 3))->toBe('$10.000')
40+
->and(Currency::Japan->format(1000, 0))->toBe('¥10');
4141
});
4242

4343
test('formats large amounts correctly', function () {
44-
expect(Currency::UnitedStates->format(1234567))->toBe('$1,234,567.00')
45-
->and(Currency::Eurozone->format(1234567))->toBe('1,234,567.00');
44+
expect(Currency::UnitedStates->format(1234567))->toBe('$12,345.67')
45+
->and(Currency::Eurozone->format(1234567))->toBe('12,345.67');
4646
});
4747

4848
test('formats small amounts correctly', function () {
49-
expect(Currency::UnitedStates->format(1))->toBe('$1.00')
49+
expect(Currency::UnitedStates->format(1))->toBe('$0.01')
5050
->and(Currency::UnitedStates->format(0))->toBe('$0.00');
5151
});
5252

5353
test('handles different currency symbol placements', function () {
54-
expect(Currency::UnitedStates->format(100))->toBe('$100.00')
55-
->and(Currency::Eurozone->format(100))->toBe('100.00');
54+
expect(Currency::UnitedStates->format(100))->toBe('$1.00')
55+
->and(Currency::Eurozone->format(100))->toBe('1.00');
5656
});
5757
});
5858

5959
describe('Currency static format method', function () {
6060
test('formats with custom symbol and default settings', function () {
61-
expect(Currency::format(1000, '$'))->toBe('$1,000.00')
62-
->and(Currency::format(1000, ''))->toBe('1,000.00')
63-
->and(Currency::format(1000, '£'))->toBe('£1,000.00');
61+
expect(Currency::format(1000, '$'))->toBe('$10.00')
62+
->and(Currency::format(1000, ''))->toBe('10.00')
63+
->and(Currency::format(1000, '£'))->toBe('£10.00');
6464
});
6565

6666
test('formats with custom precision', function () {
67-
expect(Currency::format(1000, '$', 0))->toBe('$1,000')
68-
->and(Currency::format(1000, '$', 3))->toBe('$1,000.000')
69-
->and(Currency::format(1000, '$', 1))->toBe('$1,000.0');
67+
expect(Currency::format(1000, '$', 0))->toBe('$10')
68+
->and(Currency::format(1000, '$', 3))->toBe('$10.000')
69+
->and(Currency::format(1000, '$', 1))->toBe('$10.0');
7070
});
7171

7272
test('formats with custom decimal separator', function () {
73-
expect(Currency::format(1000, '', 2, ','))->toBe('1,000,00')
74-
->and(Currency::format(1500, '$', 2, ','))->toBe('$1,500,00');
73+
expect(Currency::format(1000, '', 2, ','))->toBe('10,00')
74+
->and(Currency::format(1500, '$', 2, ','))->toBe('$15,00');
7575
});
7676

7777
test('formats with custom thousands separator', function () {
78-
expect(Currency::format(1000, '$', 2, '.', ' '))->toBe('$1 000.00')
79-
->and(Currency::format(12345, '', 2, ',', '.'))->toBe('12.345,00');
78+
expect(Currency::format(1000, '$', 2, '.', ' '))->toBe('$10.00')
79+
->and(Currency::format(12345, '', 2, ',', '.'))->toBe('123,45');
8080
});
8181

8282
test('formats with all custom parameters', function () {
83-
expect(Currency::format(12345, 'CHF', 1, ',', '.'))->toBe('CHF12.345,0')
84-
->and(Currency::format(98765, 'kr', 0, '.', ' '))->toBe('kr98 765');
83+
expect(Currency::format(12345, 'CHF', 1, ',', '.'))->toBe('CHF123,5')
84+
->and(Currency::format(98765, 'kr', 0, '.', ' '))->toBe('kr988');
8585
});
8686

8787
test('handles edge cases with zero amounts', function () {
@@ -90,8 +90,8 @@
9090
});
9191

9292
test('handles large numbers', function () {
93-
expect(Currency::format(1234567890, '$'))->toBe('$1,234,567,890.00')
94-
->and(Currency::format(1000000, '', 2, ',', '.'))->toBe('1.000.000,00');
93+
expect(Currency::format(1234567890, '$'))->toBe('$12,345,678.90')
94+
->and(Currency::format(1000000, '', 2, ',', '.'))->toBe('10.000,00');
9595
});
9696
});
9797

@@ -109,26 +109,26 @@
109109

110110
describe('Currency format real-world scenarios', function () {
111111
test('formats common financial amounts', function () {
112-
expect(Currency::UnitedStates->format(99))->toBe('$99.00')
113-
->and(Currency::UnitedStates->format(9999))->toBe('$9,999.00')
114-
->and(Currency::UnitedStates->format(10000))->toBe('$10,000.00')
115-
->and(Currency::UnitedStates->format(1000000))->toBe('$1,000,000.00');
112+
expect(Currency::UnitedStates->format(99))->toBe('$0.99')
113+
->and(Currency::UnitedStates->format(9999))->toBe('$99.99')
114+
->and(Currency::UnitedStates->format(10000))->toBe('$100.00')
115+
->and(Currency::UnitedStates->format(1000000))->toBe('$10,000.00');
116116
});
117117

118118
test('formats different currency styles', function () {
119-
expect(Currency::Brazil->format(1000))->toBe('R$1,000.00')
120-
->and(Currency::SouthAfrica->format(1000))->toBe('R1,000.00')
121-
->and(Currency::Poland->format(1000))->toBe('zł1,000.00')
122-
->and(Currency::Thailand->format(1000))->toBe('฿1,000.00');
119+
expect(Currency::Brazil->format(1000))->toBe('R$10.00')
120+
->and(Currency::SouthAfrica->format(1000))->toBe('R10.00')
121+
->and(Currency::Poland->format(1000))->toBe('zł10.00')
122+
->and(Currency::Thailand->format(1000))->toBe('฿10.00');
123123
});
124124

125125
test('supports various precision requirements', function () {
126126
// No decimals for currencies like Japanese Yen
127-
expect(Currency::Japan->format(1000, 0))->toBe('¥1,000')
127+
expect(Currency::Japan->format(1000, 0))->toBe('¥10')
128128
// Standard 2 decimals for most currencies
129-
->and(Currency::UnitedStates->format(1000, 2))->toBe('$1,000.00')
129+
->and(Currency::UnitedStates->format(1000, 2))->toBe('$10.00')
130130
// High precision for specialized use
131-
->and(Currency::UnitedStates->format(1000, 8))->toBe('$1,000.00000000');
131+
->and(Currency::UnitedStates->format(1000, 8))->toBe('$10.00000000');
132132
});
133133
});
134134
});

0 commit comments

Comments
 (0)