Skip to content

Commit

Permalink
fix mantissa #85, v3.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Babichev Maxim committed Aug 7, 2019
1 parent 2990a1d commit b8dbeff
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
7 changes: 6 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.1.5] - 2019-08-07
### Fixed
- Fixed math rounding (mantissa) #85 @anthoz69

## [3.1.4] - 2019-08-03
### Added
- Add support `barryvdh/laravel-ide-helper`
Expand Down Expand Up @@ -376,7 +380,8 @@ The operation is now executed in the transaction and updates the new `refund` fi
- Exceptions: AmountInvalid, BalanceIsEmpty.
- Models: Transfer, Transaction.

[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.4...HEAD
[Unreleased]: https://github.com/bavix/laravel-wallet/compare/3.1.5...HEAD
[3.1.5]: https://github.com/bavix/laravel-wallet/compare/3.1.4...3.1.5
[3.1.4]: https://github.com/bavix/laravel-wallet/compare/3.1.3...3.1.4
[3.1.3]: https://github.com/bavix/laravel-wallet/compare/3.1.2...3.1.3
[3.1.2]: https://github.com/bavix/laravel-wallet/compare/3.1.1...3.1.2
Expand Down
14 changes: 7 additions & 7 deletions src/Traits/HasWalletFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait HasWalletFloat
public function forceWithdrawFloat(float $amount, ?array $meta = null, bool $confirmed = true): Transaction
{
$decimalPlaces = app(WalletService::class)->decimalPlaces($this);
return $this->forceWithdraw($amount * $decimalPlaces, $meta, $confirmed);
return $this->forceWithdraw((int)round($amount * $decimalPlaces), $meta, $confirmed);
}

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

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

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

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

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

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

/**
Expand Down
53 changes: 53 additions & 0 deletions tests/WalletFloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Bavix\Wallet\Exceptions\AmountInvalid;
use Bavix\Wallet\Exceptions\BalanceIsEmpty;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Test\Models\UserFloat as User;

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

/**
* @return void
*/
public function testMantissa(): void
{
/**
* @var User $user
*/
$user = factory(User::class)->create();
$this->assertEquals($user->balance, 0);

$user->deposit(1000000);
$this->assertEquals($user->balance, 1000000);
$this->assertEquals($user->balanceFloat, 10000.00);

$transaction = $user->withdrawFloat(2556.72);
$this->assertEquals($transaction->amount, -255672);
$this->assertEquals($transaction->type, Transaction::TYPE_WITHDRAW);

$this->assertEquals($user->balance, 1000000 - 255672);
$this->assertEquals($user->balanceFloat, 10000.00 - 2556.72);

}

/**
* @return void
*/
public function testMathRounding(): void
{
/**
* @var User $user
*/
$user = factory(User::class)->create();
$this->assertEquals($user->balance, 0);

$user->deposit(1000000);
$this->assertEquals($user->balance, 1000000);
$this->assertEquals($user->balanceFloat, 10000.00);

$transaction = $user->withdrawFloat(0.2+0.1);
$this->assertEquals($transaction->amount, -30);
$this->assertEquals($transaction->type, Transaction::TYPE_WITHDRAW);

$transaction = $user->withdrawFloat(0.2+0.105);
$this->assertEquals($transaction->amount, -31);
$this->assertEquals($transaction->type, Transaction::TYPE_WITHDRAW);

$transaction = $user->withdrawFloat(0.2+0.104);
$this->assertEquals($transaction->amount, -30);
$this->assertEquals($transaction->type, Transaction::TYPE_WITHDRAW);
}

}

0 comments on commit b8dbeff

Please sign in to comment.