truncateDecimal example


  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $settings = $field_definition->getSettings();
    $precision = rand(10, 32);
    $scale = rand(0, 2);
    $max = is_numeric($settings['max']) ? $settings['max'] : pow(10, ($precision - $scale)) - 1;
    $min = is_numeric($settings['min']) ? $settings['min'] : -pow(10, ($precision - $scale)) + 1;
    // @see "Example #1 Calculate a random floating-point number" in     // http://php.net/manual/function.mt-getrandmax.php     $random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min);
    $values['value'] = self::truncateDecimal($random_decimal$scale);
    return $values;
  }

}
// Get the number of decimal digits for the $max     $decimal_digits = self::getDecimalDigits($max);
    // Do the same for the min and keep the higher number of decimal digits.     $decimal_digits = max(self::getDecimalDigits($min)$decimal_digits);
    // If $min = 1.234 and $max = 1.33 then $decimal_digits = 3     $scale = rand($decimal_digits$scale);

    // @see "Example #1 Calculate a random floating-point number" in     // http://php.net/manual/function.mt-getrandmax.php     $random_decimal = $min + mt_rand() / mt_getrandmax() * ($max - $min);
    $values['value'] = self::truncateDecimal($random_decimal$scale);
    return $values;
  }

  /** * Helper method to get the number of decimal digits out of a decimal number. * * @param int $decimal * The number to calculate the number of decimals digits from. * * @return int * The number of decimal digits. */
Home | Imprint | This part of the site doesn't use cookies.