Skip to content

Instantly share code, notes, and snippets.

@kas-cor
Created November 7, 2016 05:49
Show Gist options
  • Save kas-cor/939143e5431a957cb1b71b71c9d4dfdb to your computer and use it in GitHub Desktop.
Save kas-cor/939143e5431a957cb1b71b71c9d4dfdb to your computer and use it in GitHub Desktop.
Отбросить дробную часть числа без округления
<?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";
}
@kas-cor
Copy link
Author

kas-cor commented Nov 7, 2016

Результат:

number=1.123456, fix=1 => 1.1
number=2.12345, fix=2 => 2.12
number=2.562355, fix=3 => 2.562
number=40.1134, fix=4 => 40.1134

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment