Created
November 7, 2016 05:49
-
-
Save kas-cor/939143e5431a957cb1b71b71c9d4dfdb to your computer and use it in GitHub Desktop.
Отбросить дробную часть числа без округления
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function toFixed($number, $fix = 2) { | |
| $arr = explode('.', $number); | |
| return isset($arr[1]) ? floatval($arr[0] . "." . substr($arr[1], 0, $fix)) : $number; | |
| } | |
| // Test | |
| foreach ([ | |
| 1 => 1.123456, | |
| 2 => 2.12345, | |
| 3 => 2.562355, | |
| 4 => 40.1134, | |
| ] as $fix => $number) { | |
| echo "number=" . $number . ", fix=" . $fix . " => " . toFixed($number, $fix) . "\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Результат: