回复 57# netegg
MsgBox(0,0,Round(3.585,2))
MsgBox(0,0,Round(3.575,2))
_round(8.03,2)
Func _round($num, $dight)
$dot = StringInStr($num, '.') ;确定小数点位置
If Not $dot Then Exit ;非小数退出(或返回错误,自定)
$temp = StringLeft($num, $dot + $dight + 1) ;取出整数位+保留位数+小数点位
$temp = StringReplace($temp, '.', '') + 5 ;删除小数点,并进行要求保留位数的四舍五入
$temp = StringTrimRight($temp, 1) ;删除多余的用于四舍五入的位数
$right = StringRight($temp , $dight) ;取出小数位
$left = StringTrimRight($temp, $dight) ; 删除小数位
$str = StringRegExpReplace($left & '.' & $right, '\.$', '') ;输出并删除结果以.为结尾(即仅保留整数的四舍五入)的.
; $str = StringRegExpReplace(StringLeft($num, StringInStr($num, '.'))& StringTrimRight(StringFormat("%0" & $dight + 1 & "s", StringMid($num, $dot + 1, $dight + 1) + 5), 1), '\.$', '')
ConsoleWrite($num & @TAB & '(' & $dight & ')=====>' & @TAB & $str & @CR)
EndFunc ;==>_round
|