Created
July 22, 2015 06:03
-
-
Save imzhi/534076709291105f0045 to your computer and use it in GitHub Desktop.
四舍六入五成双
This file contains 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 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