📄 decstrround.txt
字号:
//本函数将数字格式的字符串四舍五入
//参数说明:
//string DecStr:输入待四舍五入的数字字符串
//integer Digit:保留小数位数
//boolean FillZero:少于保留位数则用0填充
/********变量定义**************************************/
integer i,p,potLt
Boolean DecError
string FrontStr,TailStr,Tempstr
integer carry
/***************************************************/
P=0
PotLt=0
DecError=false
Tempstr=''
if DecStr='.' then//如果以小数点为首的数字格式
DecStr='0'
else
If mid(DecStr,1,1)='.' then
DecStr='0'+DecStr
end if
end if
//先判断数字是否合法
for i=1 to len(DecStr)
if mid(Decstr,i,1)='.' then//记录小数点的个数和位置
p=p+1
potLt=i
end if
if (mid(Decstr,i,1)<'0') and (Mid(Decstr,i,1)<>'.') and (mid(Decstr,i,1)>'9') then
DecError=true //判断有无非法字符
Exit
end if
next
if P>1 then DecError=true //如果小数点超过1则数字非法
if DecError then
messagebox('参数无效','输入参数无效');
return('');
end if
if p=0 then //如果为纯整数,直接输出
Tempstr=Decstr
if FillZero then//如果需要补0,则补足小数部分的空位为0
TempStr=Tempstr+'.'
for i=1 to digit
Tempstr=Tempstr+'0'
next
end if
return(Tempstr)
end if
//如果存在小数,分别就整数段和小数段分析
FrontStr=mid(Decstr,1,potLt -1)
Tailstr=mid(Decstr,PotLt+1,Len(Decstr) -PotLt)
do while mid(Tailstr,Len(TailStr),1)='0'// 去掉小数位的后面的零字符
TailStr=mid(tailStr,1,Len(TailStr)-1)
loop
//如果保留小数位数大于等于目前的小数位数
if digit>=Len(Tailstr) then
if FillZero then
for i=1 to Digit -Len(Tailstr) //如果需要补0,则补足小数部分的空位为0
Decstr=Decstr+'0'
next
end if
return(Decstr)
end if
Tempstr=''
Carry=0
if integer(mid(Tailstr,Digit+1,1))>=5 then Carry=1
//先从小数开始分析,四舍五入
for i=digit to 1 step -1
p=integer(mid(Tailstr,i,1))+Carry
if p=10 then
if FillZero then
tempstr=TempStr+'0'//自动填0
end if
Carry=1//形成进位
else
Tempstr=Tempstr+string(p)
if not FillZero then
if Tempstr='0' then Tempstr=''//不想填0时,如果返回结果等于0,则清空结果
end if
Carry=0
end if
next
if Tempstr<>'' then Tempstr=Tempstr+'.'//如果小数数位不为空,则加上小数点
//小数部分分析完毕,开始分析整数部分
for i=len(FrontStr) to 1 step -1
p=integer(mid(Frontstr,i,1))+Carry
if p=10 then
Tempstr=Tempstr+'0'
Carry=1
else
Tempstr=Tempstr+string(p)
Carry=0
end if
next
if Carry=1 then Tempstr=Tempstr+'1'//到最后还存在进位,则进1
//处理所得到的结果字符串--倒置处理,得到最后的结果
FrontStr=''
for i=Len(Tempstr) to 1 step -1
FrontStr=FrontStr+mid(Tempstr,i,1)
next
return(frontStr)//返回最终结果
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -