2014-03-26 37 views
0

我用我的SQL Server格式号报道了简单的VBScript命令格式化一些数字字段:默认NumDigAfterDec并没有四舍五入

FormatNumber(value,,-1,0,-1) 

重要,我是第二个参数NumDigAfterDec被设置为默认,意思是 ,即“使用计算机的区域设置”(http://www.w3schools.com/vbscript/func_formatnumber.asp)。这正是我所喜欢的。但是,如果当前数字在小数点后有更多数字,则会进行四舍五入。在这种情况下,我想看看所有的地方。

例如对于小数点后两位,我想:

0  0.00 
90.7 90.70 
1.2345 1.2345 (instead of 1.23) 

这可能没有在我的报告中编写代码?

回答

1

如果我理解正确,你想要一个条件来说明小数的长度是否大于2个地方,那么显示完整的十进制数,否则显示2个小数位?

一个简单的方法来做到这一点是:

Dim value : value = "100.54367" 
Dim GetDecCount : GetDecCount = Len(Mid(Value, instr(value, ".")+1, len(value))) 
if GetDecCount>2 then 
msgbox "This number exceeds the regional decimal points of 2, with a length of " & GetDecCount & " decimal points." 
else 
FormatNumber(value,,-1,0,-1) 
end if 
1

尝试是这样的:

Set re = New RegExp 
re.Pattern = ".\d{3,}$" 

if re.Test(value) Then 
    fnum = CStr(value) 
Else 
    fnum = FormatNumber(value, , -1, 0, -1) 
End If