Adding decimal lmit

This commit is contained in:
Dennis Eichhorn 2016-03-03 13:32:49 +01:00
parent 5d82556f9a
commit 9140f02439

View File

@ -3,16 +3,19 @@
namespace phpOMS\Localization; namespace phpOMS\Localization;
class Money { class Money {
const DECIMALS = 5;
private static function getFromInt(int $value, string $currency, string $thousands = ',', string $decimal = '.', int $decimals = 2) : string private static function getFromInt(int $value, string $currency, string $thousands = ',', string $decimal = '.', int $decimals = 2) : string
{ {
if($decimals > ISO4270::{$currency}) { if($decimals > ISO4270::{$currency}) {
$decimals = ISO4270::{$currency}; $decimals = ISO4270::{$currency};
} }
$value = (string) round($value, - 5 + $decimals); $value = (string) round($value, - self::DECIMALS + $decimals);
$left = substr($value, 0, -5); $left = substr($value, 0, -self::DECIMALS);
$right = substr($value, -5); $right = substr($value, -self::DECIMALS);
return ($decimals > 0) : number_format($left, 0, $thousands, $decimal); . $decimal . $right : (string) $left; return ($decimals > 0) : number_format($left, 0, $thousands, $decimal); . $decimal . $right : (string) $left;
} }
@ -34,8 +37,8 @@ class Money {
$right = $split[1]; $right = $split[1];
} }
$right = substr($right, 0, -5); $right = substr($right, 0, -self::DECIMALS);
$value = (int) round((int) $left + (int) $right, - 5 + $decimals); $value = (int) round((int) $left + (int) $right, - self::DECIMALS + $decimals);
return $value; return $value;
} }