= 0) { $nearest = (int) floor(abs($magnitude) / 3); $symbol = $prefixes_pos[$nearest]; } else { $nearest = (int) round(abs($magnitude) / 3); $symbol = $prefixes_neg[$nearest]; } if ($magnitude < 0) { $nearest *= -1; } return [10 ** (3 * $nearest), $symbol]; } /** * * @param float $value * @return array */ public function convertValue(float $value) : array { //Choose the prefix to use $tmp = $this->getPrefixByMagnitude($this->getMagnitude($value)); $ret = array( 'value' => $value / $tmp[0], 'prefix_magnitude' => log10($tmp[0]), 'prefix' => $tmp[1] ); return $ret; } /** * Formats the given value to a string, using the given options * @param float $value The value that should be converted * @param string $unit The unit that should be appended after the prefix * @param int $decimals The number of decimals (after decimal dot) that should be outputed. * @return string */ public function format(float $value, string $unit = '', int $decimals = 2) : string { [$divisor, $symbol] = $this->getPrefixByMagnitude($this->getMagnitude($value)); $value /= $divisor; //Build the format string, e.g.: %.2d km if ($unit !== '' || $symbol !== '') { $format_string = '%.' . $decimals . 'f ' . $symbol . $unit; } else { $format_string = '%.' . $decimals . 'f'; } return sprintf($format_string, $value); } }