/** * return the monetary value represented by this object converted to its base units * * @return float */ public function getConvertedAmount() { return round($this->amount / $this->currency->getSubUnit(), $this->currency->getDefaultFractionDigits()); }
/** * @param $value * @param Currency $currency * @return Money * @throws \Exception */ protected function string2money($value, $currency) { $parts = explode('/', $value); if (!preg_match('/^-?[0-9]+$/', $parts[0])) { throw new \Exception('Illegal currency value: ' . $value . ' for currency ' . $currency); } if (isset($parts[1])) { $div = (int) $parts[1]; if ($div === $currency->getSubUnit()) { $moneyValue = (int) $parts[0]; } elseif ($div < $currency->getSubUnit()) { $moneyValue = (int) $parts[0] * $currency->getSubUnit() / $div; if (!is_int($moneyValue)) { throw new \Exception('Illegal currency value: ' . $value . ' for currency ' . $currency . '. Value conversion resulted in a float value'); } } else { throw new \Exception('Unknown currency value: ' . $value . ' for currency ' . $currency); } } else { throw new \Exception('Unknown currency value: ' . $value . ' for currency ' . $currency); } return new Money($moneyValue, $currency); }
/** * Converts an int to a string for the database reporting etc * @param int $value * @param \SebastianBergmann\Money\Currency $currency * @return string */ function convertIntToString($value, Currency $currency) { $subunit = $currency->getSubUnit(); return (string) round($value / $subunit, log10($subunit), PHP_ROUND_HALF_UP); }