Exemplo n.º 1
1
 /**
  * {@inheritdoc}
  */
 public function convertToPHPValue($value, AbstractPlatform $platform)
 {
     if ($value === null) {
         return null;
     }
     return BigDecimal::of($value);
 }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 3
0
 /**
  * @dataProvider providerToFloat
  *
  * @param string $value The big decimal value.
  * @param float  $float The expected float value.
  */
 public function testToFloat($value, $float)
 {
     $this->assertSame($float, BigDecimal::of($value)->toFloat());
 }
Exemplo n.º 4
0
 /**
  * Parses a string representation of a money as returned by `__toString()`, e.g. "USD 23.00".
  *
  * @param string $string
  *
  * @return Money
  *
  * @throws MoneyParseException      If the parsing fails.
  * @throws UnknownCurrencyException If the currency code is not known.
  */
 public static function parse($string)
 {
     $pos = strrpos($string, ' ');
     if ($pos === false) {
         throw MoneyParseException::invalidFormat($string);
     }
     $currency = substr($string, 0, $pos);
     $amount = substr($string, $pos + 1);
     $currency = Currency::of($currency);
     try {
         $amount = BigDecimal::of($amount);
     } catch (ArithmeticException $e) {
         throw MoneyParseException::wrap($e);
     }
     return new Money($amount, $currency);
 }
Exemplo n.º 5
0
 /**
  * Returns the quotient and remainder of the division of this number by the given one.
  *
  * The quotient has a scale of `0`, and the remainder has a scale of `max($this->scale, $that->scale)`.
  *
  * @param BigNumber|number|string $that The divisor. Must be convertible to a BigDecimal.
  *
  * @return BigDecimal[] An array containing the quotient and the remainder.
  *
  * @throws ArithmeticException If the divisor is not a valid decimal number, or is zero.
  */
 public function quotientAndRemainder($that)
 {
     $that = BigDecimal::of($that);
     if ($that->isZero()) {
         throw DivisionByZeroException::divisionByZero();
     }
     $p = $this->valueWithMinScale($that->scale);
     $q = $that->valueWithMinScale($this->scale);
     list($quotient, $remainder) = Calculator::get()->divQR($p, $q);
     $scale = $this->scale > $that->scale ? $this->scale : $that->scale;
     $quotient = new BigDecimal($quotient, 0);
     $remainder = new BigDecimal($remainder, $scale);
     return [$quotient, $remainder];
 }
Exemplo n.º 6
0
 /**
  * @param number|string|null $step The step, or null to remove it.
  *
  * @return static
  *
  * @throws \InvalidArgumentException If the step is not a valid number or not positive.
  */
 public function setStep($step)
 {
     if ($step !== null) {
         $step = BigDecimal::of($step);
         if ($step->isNegativeOrZero()) {
             throw new \InvalidArgumentException('The number validator step must be strictly positive.');
         }
     }
     $this->step = $step;
     return $this;
 }