From 09e48a05b98b94fd52dc2562089f67e8d1279298 Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Tue, 4 Feb 2020 09:37:24 +0100 Subject: update to 2.0.0 (alpha) raise dependency on PHP 7.2 fix undefined symbol using patch from https://github.com/php-decimal/ext-decimal/pull/34 disable ZTS build which segfaults, reported as https://github.com/php-decimal/ext-decimal/issues/35 --- Number.php | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 Number.php (limited to 'Number.php') diff --git a/Number.php b/Number.php new file mode 100644 index 0000000..07ca222 --- /dev/null +++ b/Number.php @@ -0,0 +1,132 @@ +value : $number; + + if ($value === "INF") $value = INF; + else if ($value === "-INF") $value = -INF; + else if ($value === "NAN") $value = NAN; + + return $value; + } + + protected function __construct($value) + { + $this->value = self::parse($value); + } + + public static function valueOf($value): \Decimal\Number + { + return new static($value); + } + + public function add($other): \Decimal\Number + { + printf("%s\n", __METHOD__); + + return new static($this->value + $this->parse($other)); + } + + public function sub($other): \Decimal\Number + { + printf("%s\n", __METHOD__); + + return new static($this->value - $this->parse($other)); + } + + public function mul($other): \Decimal\Number + { + printf("%s\n", __METHOD__); + + return new static($this->value * $this->parse($other)); + } + + public function div($other): \Decimal\Number + { + printf("%s\n", __METHOD__); + + return new static($this->value / $this->parse($other)); + } + + public function pow($other): \Decimal\Number + { + printf("%s\n", __METHOD__); + + return new static($this->value ** $this->parse($other)); + } + + public function mod($other): \Decimal\Number + { + printf("%s\n", __METHOD__); + + return new static($this->value % $this->parse($other)); + } + + public function shiftl($places): \Decimal\Number + { + printf("%s\n", __METHOD__); + + return new static($this->value * (10 ** $places)); + } + + public function shiftr($places): \Decimal\Number + { + printf("%s\n", __METHOD__); + + return new static($this->value / (10 ** $places)); + } + + public function round(int $places = NULL, int $mode = NULL): \Decimal\Number + { + return new static($this->toDecimal(\Decimal\Decimal::MAX_PRECISION)->round($places, $mode)->toString()); + } + + public function toFixed(int $places = NULL, bool $commas = NULL, int $mode = NULL): string + { + return new static($this->toDecimal(\Decimal\Decimal::MAX_PRECISION)->toFixed($places, $commas, $mode)); + } + + public function toDecimal(int $precision): \Decimal\Decimal + { + printf("%s\n", __METHOD__); + + return parent::toDecimal($precision); + } + + public function toRational(): \Decimal\Rational + { + printf("%s\n", __METHOD__); + + return parent::toRational(); + } + + public function toString(): string + { + return (string) $this->value; + } + + public function toInt(): int + { + return (int) $this->value; + } + + public function toFloat(): float + { + return (float) $this->value; + } + + public function compareTo($other): int + { + printf("%s\n", __METHOD__); + + return $this->value <=> self::parse($other); + } +} -- cgit