Skip to content

Instantly share code, notes, and snippets.

@imzhi
Created July 22, 2015 06:03
Show Gist options
  • Save imzhi/534076709291105f0045 to your computer and use it in GitHub Desktop.
Save imzhi/534076709291105f0045 to your computer and use it in GitHub Desktop.
四舍六入五成双
<?php
function five2pair($number)
{
$num_str = strval($number);
if (!strpos($num_str, '.')) $num_str .= '.';
list($int, $dec) = explode('.', $num_str);
$new_num = floatval( $int . '.' . substr($dec, 0, 2) );
if (isset($dec{2})) {
$fen = $dec{2};
if ($fen == 5) {
if (strlen($dec) > 3) {
$end_part = substr($dec, 3);
if (intval($end_part)) {
$new_num = $new_num + 0.01;
}
} else {
if (intval($dec{1}) % 2) {
$new_num = $new_num + 0.01;
}
}
} elseif ($fen >= 6) {
$new_num = $new_num + 0.01;
}
}
return number_format($new_num, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment