mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-08 13:28:39 +00:00
Adding basic arithmetic
This commit is contained in:
parent
f397142aab
commit
aef5b006c7
|
|
@ -23,12 +23,21 @@ class Money {
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getInt() : int {
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
public function setString(string $value) {
|
public function setString(string $value) {
|
||||||
|
$this->value = self::toInt($value, $this->decimal);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function toInt(string $value, string $decimal = ',') : int
|
||||||
|
{
|
||||||
$split = explode($value, $decimal);
|
$split = explode($value, $decimal);
|
||||||
|
|
||||||
$left = '';
|
$left = '';
|
||||||
$left = $split[0];
|
$left = $split[0];
|
||||||
$left = str_replace($thousands, '', $left);
|
$left = str_replace($this->thousands, '', $left);
|
||||||
|
|
||||||
$rigth = '';
|
$rigth = '';
|
||||||
if(count($split) > 1) {
|
if(count($split) > 1) {
|
||||||
|
|
@ -36,20 +45,64 @@ class Money {
|
||||||
}
|
}
|
||||||
|
|
||||||
$right = substr($right, 0, -self::DECIMALS);
|
$right = substr($right, 0, -self::DECIMALS);
|
||||||
$this->value = (int) round((int) $left + (int) $right, - self::DECIMALS + $decimals);
|
$this->value = (int) $left * 100000 + (int) $right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAmount(int $decimals = 2) : string
|
public function getAmount(int $decimals = 2) : string
|
||||||
{
|
{
|
||||||
if($decimals > ISO4270::{$currency}) {
|
if($decimals > ISO4270::{$this->currency}) {
|
||||||
$decimals = ISO4270::{$currency};
|
$decimals = ISO4270::{$this->currency};
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = (string) round($value, - self::DECIMALS + $decimals);
|
$value = (string) round($value, - self::DECIMALS + $this->decimals);
|
||||||
|
|
||||||
$left = substr($value, 0, -self::DECIMALS);
|
$left = substr($value, 0, -self::DECIMALS);
|
||||||
$right = substr($value, -self::DECIMALS);
|
$right = substr($value, -self::DECIMALS);
|
||||||
|
|
||||||
return ($decimals > 0) : number_format($left, 0, $this->thousands, $this->decimal); . $decimal . $right : (string) $left;
|
return ($decimals > 0) : number_format($left, 0, $this->thousands, $this->decimal); . $this->decimal . $right : (string) $left;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add($value)
|
||||||
|
{
|
||||||
|
if(is_string($value) || is_float($value)) {
|
||||||
|
$this->value += self::toInt((string) $value);
|
||||||
|
} elseif(is_int($value)) {
|
||||||
|
$this->value += $value;
|
||||||
|
} elseif($value instanceof Money) {
|
||||||
|
$this->value += $value->getInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sub($value)
|
||||||
|
{
|
||||||
|
if(is_string($value) || is_float($value)) {
|
||||||
|
$this->value -= self::toInt((string) $value);
|
||||||
|
} elseif(is_int($value)) {
|
||||||
|
$this->value -= $value;
|
||||||
|
} elseif($value instanceof Money) {
|
||||||
|
$this->value -= $value->getInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function mult($value)
|
||||||
|
{
|
||||||
|
if(is_string($value) || is_float($value)) {
|
||||||
|
$this->value *= self::toInt((string) $value);
|
||||||
|
} elseif(is_int($value)) {
|
||||||
|
$this->value *= $value;
|
||||||
|
} elseif($value instanceof Money) {
|
||||||
|
$this->value *= $value->getInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function div($value)
|
||||||
|
{
|
||||||
|
if(is_string($value) || is_float($value)) {
|
||||||
|
$this->value = self::toInt((string) ($this->value / self::toInt((string) $value)));
|
||||||
|
} elseif(is_int($value)) {
|
||||||
|
$this->value = self::toInt((string) ($this->value / $value));
|
||||||
|
} elseif($value instanceof Money) {
|
||||||
|
$this->value = self::toInt((string) ($this->value / $value->getInt()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user