2014-03-26 38 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