Skip to content

Commit b8dbeff

Browse files
author
Babichev Maxim
committed
fix mantissa #85, v3.1.5
1 parent 2990a1d commit b8dbeff

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [3.1.5] - 2019-08-07
10+
### Fixed
11+
- Fixed math rounding (mantissa) #85 @anthoz69
12+
913
## [3.1.4] - 2019-08-03
1014
### Added
1115
- Add support `barryvdh/laravel-ide-helper`
@@ -376,7 +380,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
376380
- Exceptions: AmountInvalid, BalanceIsEmpty.
377381
- Models: Transfer, Transaction.
378382

379-
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.4...HEAD
383+
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.5...HEAD
384+
[3.1.5]: https://github.com/bavix/laravel-wallet/compare/3.1.4...3.1.5
380385
[3.1.4]: https://github.com/bavix/laravel-wallet/compare/3.1.3...3.1.4
381386
[3.1.3]: https://github.com/bavix/laravel-wallet/compare/3.1.2...3.1.3
382387
[3.1.2]: https://github.com/bavix/laravel-wallet/compare/3.1.1...3.1.2

src/Traits/HasWalletFloat.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ trait HasWalletFloat
2828
public function forceWithdrawFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction
2929
{
3030
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
31-
return $this->forceWithdraw($amount * $decimalPlaces, $meta, $confirmed);
31+
return $this->forceWithdraw((int)round($amount * $decimalPlaces), $meta, $confirmed);
3232
}
3333

3434
/**
@@ -41,7 +41,7 @@ public function forceWithdrawFloat(float $amount, ?array $meta = null, bool $con
4141
public function depositFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction
4242
{
4343
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
44-
return $this->deposit($amount * $decimalPlaces, $meta, $confirmed);
44+
return $this->deposit((int)round($amount * $decimalPlaces), $meta, $confirmed);
4545
}
4646

4747
/**
@@ -54,7 +54,7 @@ public function depositFloat(float $amount, ?array $meta = null, bool $confirmed
5454
public function withdrawFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction
5555
{
5656
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
57-
return $this->withdraw($amount * $decimalPlaces, $meta, $confirmed);
57+
return $this->withdraw((int)round($amount * $decimalPlaces), $meta, $confirmed);
5858
}
5959

6060
/**
@@ -64,7 +64,7 @@ public function withdrawFloat(float $amount, ?array $meta = null, bool $confirme
6464
public function canWithdrawFloat(float $amount): bool
6565
{
6666
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
67-
return $this->canWithdraw($amount * $decimalPlaces);
67+
return $this->canWithdraw((int)round($amount * $decimalPlaces));
6868
}
6969

7070
/**
@@ -77,7 +77,7 @@ public function canWithdrawFloat(float $amount): bool
7777
public function transferFloat(Wallet $wallet, float $amount, ?array $meta = null): Transfer
7878
{
7979
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
80-
return $this->transfer($wallet, $amount * $decimalPlaces, $meta);
80+
return $this->transfer($wallet, (int)round($amount * $decimalPlaces), $meta);
8181
}
8282

8383
/**
@@ -89,7 +89,7 @@ public function transferFloat(Wallet $wallet, float $amount, ?array $meta = null
8989
public function safeTransferFloat(Wallet $wallet, float $amount, ?array $meta = null): ?Transfer
9090
{
9191
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
92-
return $this->safeTransfer($wallet, $amount * $decimalPlaces, $meta);
92+
return $this->safeTransfer($wallet, (int)round($amount * $decimalPlaces), $meta);
9393
}
9494

9595
/**
@@ -101,7 +101,7 @@ public function safeTransferFloat(Wallet $wallet, float $amount, ?array $meta =
101101
public function forceTransferFloat(Wallet $wallet, float $amount, ?array $meta = null): Transfer
102102
{
103103
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
104-
return $this->forceTransfer($wallet, $amount * $decimalPlaces, $meta);
104+
return $this->forceTransfer($wallet, (int)round($amount * $decimalPlaces), $meta);
105105
}
106106

107107
/**

tests/WalletFloatTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Bavix\Wallet\Exceptions\AmountInvalid;
66
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
7+
use Bavix\Wallet\Models\Transaction;
78
use Bavix\Wallet\Test\Models\UserFloat as User;
89

910
class WalletFloatTest extends TestCase
@@ -190,4 +191,56 @@ public function testConfirmed(): void
190191
$this->assertEquals($user->balanceFloat, 0);
191192
}
192193

194+
/**
195+
* @return void
196+
*/
197+
public function testMantissa(): void
198+
{
199+
/**
200+
* @var User $user
201+
*/
202+
$user = factory(User::class)->create();
203+
$this->assertEquals($user->balance, 0);
204+
205+
$user->deposit(1000000);
206+
$this->assertEquals($user->balance, 1000000);
207+
$this->assertEquals($user->balanceFloat, 10000.00);
208+
209+
$transaction = $user->withdrawFloat(2556.72);
210+
$this->assertEquals($transaction->amount, -255672);
211+
$this->assertEquals($transaction->type, Transaction::TYPE_WITHDRAW);
212+
213+
$this->assertEquals($user->balance, 1000000 - 255672);
214+
$this->assertEquals($user->balanceFloat, 10000.00 - 2556.72);
215+
216+
}
217+
218+
/**
219+
* @return void
220+
*/
221+
public function testMathRounding(): void
222+
{
223+
/**
224+
* @var User $user
225+
*/
226+
$user = factory(User::class)->create();
227+
$this->assertEquals($user->balance, 0);
228+
229+
$user->deposit(1000000);
230+
$this->assertEquals($user->balance, 1000000);
231+
$this->assertEquals($user->balanceFloat, 10000.00);
232+
233+
$transaction = $user->withdrawFloat(0.2+0.1);
234+
$this->assertEquals($transaction->amount, -30);
235+
$this->assertEquals($transaction->type, Transaction::TYPE_WITHDRAW);
236+
237+
$transaction = $user->withdrawFloat(0.2+0.105);
238+
$this->assertEquals($transaction->amount, -31);
239+
$this->assertEquals($transaction->type, Transaction::TYPE_WITHDRAW);
240+
241+
$transaction = $user->withdrawFloat(0.2+0.104);
242+
$this->assertEquals($transaction->amount, -30);
243+
$this->assertEquals($transaction->type, Transaction::TYPE_WITHDRAW);
244+
}
245+
193246
}

0 commit comments

Comments
 (0)