2019-08-16 16:43:31 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-12-18 17:28:42 +01:00
|
|
|
namespace App\Services\Formatters;
|
2019-08-16 16:43:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A service that helps you to format values using the SI prefixes.
|
|
|
|
*/
|
|
|
|
class SIFormatter
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns the magnitude of a value (the count of decimal place of the highest decimal place).
|
|
|
|
* For example, for 100 (=10^2) this function returns 2. For -2500 (=-2.5*10^3) this function returns 3.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param float $value the value of which the magnitude should be determined
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2019-08-16 16:43:31 +02:00
|
|
|
* @return int The magnitude of the value
|
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getMagnitude(float $value): int
|
2019-08-16 16:43:31 +02:00
|
|
|
{
|
|
|
|
return (int) floor(log10(abs($value)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the best SI prefix (and its corresponding divisor) for the given magnitude.
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param int $magnitude the magnitude for which the prefix should be determined
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
2023-04-15 23:14:53 +02:00
|
|
|
* @return array An array, containing the divisor in first element, and the prefix symbol in second. For example, [1000, "k"].
|
2019-08-16 16:43:31 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function getPrefixByMagnitude(int $magnitude): array
|
2019-08-16 16:43:31 +02:00
|
|
|
{
|
2019-11-09 00:47:20 +01:00
|
|
|
$prefixes_pos = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
|
|
|
|
$prefixes_neg = ['', 'm', 'μ', 'n', 'p', 'f', 'a', 'z', 'y'];
|
2019-08-16 16:43:31 +02:00
|
|
|
|
|
|
|
if ($magnitude >= 0) {
|
2019-09-22 23:47:40 +02:00
|
|
|
$nearest = (int) floor(abs($magnitude) / 3);
|
2019-08-16 16:43:31 +02:00
|
|
|
$symbol = $prefixes_pos[$nearest];
|
|
|
|
} else {
|
2019-09-22 23:47:40 +02:00
|
|
|
$nearest = (int) round(abs($magnitude) / 3);
|
2019-08-16 16:43:31 +02:00
|
|
|
$symbol = $prefixes_neg[$nearest];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($magnitude < 0) {
|
|
|
|
$nearest *= -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [10 ** (3 * $nearest), $symbol];
|
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
public function convertValue(float $value): array
|
2019-08-24 12:55:47 +02:00
|
|
|
{
|
|
|
|
//Choose the prefix to use
|
|
|
|
$tmp = $this->getPrefixByMagnitude($this->getMagnitude($value));
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
return [
|
2019-08-24 12:55:47 +02:00
|
|
|
'value' => $value / $tmp[0],
|
|
|
|
'prefix_magnitude' => log10($tmp[0]),
|
2019-11-09 00:47:20 +01:00
|
|
|
'prefix' => $tmp[1],
|
|
|
|
];
|
2019-08-24 12:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* 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
|
2020-01-04 20:24:09 +01:00
|
|
|
* @param int $decimals the number of decimals (after decimal dot) that should be outputed
|
2020-05-10 21:39:31 +02:00
|
|
|
*
|
2020-04-14 17:21:30 +02:00
|
|
|
* @return string The formatted value
|
2019-08-16 16:43:31 +02:00
|
|
|
*/
|
2019-11-09 00:47:20 +01:00
|
|
|
public function format(float $value, string $unit = '', int $decimals = 2): string
|
2019-08-16 16:43:31 +02:00
|
|
|
{
|
|
|
|
[$divisor, $symbol] = $this->getPrefixByMagnitude($this->getMagnitude($value));
|
|
|
|
$value /= $divisor;
|
|
|
|
//Build the format string, e.g.: %.2d km
|
2023-06-11 14:15:46 +02:00
|
|
|
$format_string = '' !== $unit || '' !== $symbol ? '%.'.$decimals.'f '.$symbol.$unit : '%.'.$decimals.'f';
|
2019-08-16 16:43:31 +02:00
|
|
|
|
|
|
|
return sprintf($format_string, $value);
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|