PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php // Есть функция которая принимает $sum и $decimal function roundString(string $sum, int $decimal): string { $dotPosition = strpos($sum, '.'); if ($dotPosition === false) { return $sum; } return substr(str_pad($sum, $dotPosition + $decimal, '0'), 0, $dotPosition + $decimal + 1); } // Нужно получить суммы без округления: // 0.0000003160360000 (decimal = 8) => 0.00000031 echo roundString('0.0000003160360000', 8) . PHP_EOL; //0.0134607618791220 (decimal = 8) => 0.01346076 echo roundString('0.0134607618791220', 8) . PHP_EOL; //74.29420505200595 (decimal = 2) => 74.29 echo roundString('74.29420505200595', 2) . PHP_EOL; //3164201.202 (decimal = 2) => 3164201.20 echo roundString('3164201.202', 2) . PHP_EOL; //1.0000000000000000 (decimal = 2) => 1 echo roundString('1.0000000000000000', 2) . PHP_EOL; //0.000023844881 (decimal = 8) => 0.00002384 echo roundString('0.000023844881', 8) . PHP_EOL; //41937.7233986 (decimal = 2) => 41937.72 echo roundString('41937.7233986', 2) . PHP_EOL; //12345.00000 (decimal = 2) => 12345 echo roundString('12345.00000', 2) . PHP_EOL;
Show:  
Copy Clear