public static function main($argc, $argv) { if ($argc < 2) { echo 'Usage: php test.php number_of_files_to_copy' . PHP_EOL; exit; } $numFilesToCopy = (int) $argv[1]; if ($numFilesToCopy < 1) { $numFilesToCopy = 1; } $fileName = 'test.yml'; $filePath = __DIR__ . '/' . $fileName; $yamlArr = Yaml::parse(file_get_contents($filePath)); $newFilenamePrefix = $yamlArr['new_filename_prefix']; try { $filesystem = new Filesystem(); for ($i = 1; $i <= $numFilesToCopy; $i++) { $newFilepath = __DIR__ . "/{$newFilenamePrefix}_{$i}.yml"; $filesystem->copy($filePath, $newFilepath); } } catch (FileNotFoundException $e) { echo "File '{$filePath}' could not be found!" . PHP_EOL; } catch (IOException $e) { echo "Could not copy file '{$filePath}' to '{$newFilepath}'!" . PHP_EOL; } finally { // Just for fun unset($filesystem); } $funny = (int) ((0.7 + 0.1) * 10); echo $funny . PHP_EOL; $notFunny = BigDecimal::of(0.7)->plus(BigDecimal::of(0.1))->multipliedBy(10)->toInteger(); echo $notFunny . PHP_EOL; $notFunny = BigRational::of('7/10')->plus(BigRational::of('1/10'))->multipliedBy(10)->toInteger(); echo $notFunny . PHP_EOL; }
/** * {@inheritdoc} */ public function getExchangeRate($sourceCurrencyCode, $targetCurrencyCode) { if ($sourceCurrencyCode == $this->baseCurrencyCode) { return $this->provider->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode); } if ($targetCurrencyCode == $this->baseCurrencyCode) { $exchangeRate = $this->provider->getExchangeRate($targetCurrencyCode, $sourceCurrencyCode); return BigRational::of($exchangeRate)->reciprocal(); } $baseToSource = $this->provider->getExchangeRate($this->baseCurrencyCode, $sourceCurrencyCode); $baseToTarget = $this->provider->getExchangeRate($this->baseCurrencyCode, $targetCurrencyCode); return BigRational::of($baseToTarget)->dividedBy($baseToSource); }
/** * Returns the result of the division of this number by the given one. * * @param BigNumber|number|string $that The divisor. * * @return BigRational The result. * * @throws ArithmeticException If the divisor is not a valid number, or is zero. */ public function dividedBy($that) { $that = BigRational::of($that); $numerator = $this->numerator->multipliedBy($that->denominator); $denominator = $this->denominator->multipliedBy($that->numerator); return new BigRational($numerator, $denominator, true); }
/** * @return array */ public function providerOf() { return [['USD 1.00', 1, 'USD'], ['JPY 1', 1.0, 'JPY'], ['JPY 1.200', '1.2', 'JPY', 3], ['EUR 0.42', BigRational::of('3/7'), 'EUR', null, RoundingMode::DOWN], ['EUR 0.43', BigRational::of('3/7'), 'EUR', null, RoundingMode::UP], ['CUSTOM 0.428', BigRational::of('3/7'), Currency::create('CUSTOM', 0, '', 3), null, RoundingMode::DOWN], ['CUSTOM 0.4286', BigRational::of('3/7'), Currency::create('CUSTOM', 0, '', 3), 4, RoundingMode::UP], [RoundingNecessaryException::class, '1.2', 'JPY'], [NumberFormatException::class, '1.', 'JPY']]; }
/** * {@inheritdoc} */ public function toBigRational() { return BigRational::create($this, BigInteger::one(), false); }
/** * @param string $numerator The expected numerator, as a string. * @param string $denominator The expected denominator, as a string. * @param BigRational $actual The BigRational instance to test. */ protected final function assertBigRationalInternalValues($numerator, $denominator, $actual) { $this->assertInstanceOf(BigRational::getNamespace(), $actual); $this->assertSame($numerator, (string) $actual->numerator()); $this->assertSame($denominator, (string) $actual->denominator()); }
/** * {@inheritdoc} */ public function toBigRational() { $numerator = BigInteger::create($this->value); $denominator = BigInteger::create('1' . str_repeat('0', $this->scale)); return BigRational::create($numerator, $denominator, false); }
/** * @expectedException \LogicException */ public function testDirectCallToUnserialize() { BigRational::nd(1, 2)->unserialize('123/456'); }
/** * @dataProvider providerGetExchangeRate * * @param string $sourceCurrencyCode The code of the source currency. * @param string $targetCurrencyCode The code of the target currency. * @param string $exchangeRate The expected exchange rate, rounded DOWN to 6 decimals. */ public function testGetExchangeRate($sourceCurrencyCode, $targetCurrencyCode, $exchangeRate) { $rate = $this->getExchangeRateProvider()->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode); $this->assertSame($exchangeRate, (string) BigRational::of($rate)->toScale(6, RoundingMode::DOWN)); }