/** * Converts a string price from the database (e.g. "19.90" in dollars) into an int * * The function is designed to take into account the errors that can arise from floating point numbers * * If the number of decimals of the $value variable is more than the number of frational digits. e.g. 1000.60 Yen * Then the number will be rounded to the number of fractional digits. e.g the value of 1000.60 Yen is just 1000.00 * * @param float|string $value * @param \SebastianBergmann\Money\Currency $currency * @return int */ function convertStringToInt($value, Currency $currency) { $currencyMultiplier = $currency->getSubUnit(); $valueInCentsAsFloat = $currencyMultiplier * round($value, $currency->getDefaultFractionDigits(), PHP_ROUND_HALF_UP); $valueInCentsAsFloatRounded = round($valueInCentsAsFloat, 0, PHP_ROUND_HALF_UP); return (int) $valueInCentsAsFloatRounded; }
/** * 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()); }