簡單來說,VB6的 Integer,Long型態,或CInt,CLng,Round等函數都是四捨六入五逢單才進位
所以要有四捨五入功能就只能用Format
下面這段code跑下去就能了解
Debug.Print "CInt(1.5)=" & CInt(1.5)
Debug.Print "CInt(1.6)=" & CInt(1.6)
Debug.Print "CInt(2.5)=" & CInt(2.5)
Debug.Print "CInt(2.6)=" & CInt(2.6)
Debug.Print "CInt(3.5)=" & CInt(3.5)
Debug.Print "CInt(3.6)=" & CInt(3.6)
Debug.Print "CInt(4.5)=" & CInt(4.5)
Debug.Print "CInt(4.6)=" & CInt(4.6)
Debug.Print "CLng(1.5)=" & CLng(1.5)
Debug.Print "CLng(1.6)=" & CLng(1.6)
Debug.Print "CLng(2.5)=" & CLng(2.5)
Debug.Print "CLng(2.6)=" & CLng(2.6)
Debug.Print "CLng(3.5)=" & CLng(3.5)
Debug.Print "CLng(3.6)=" & CLng(3.6)
Debug.Print "CLng(4.5)=" & CLng(4.5)
Debug.Print "CLng(4.6)=" & CLng(4.6)
Debug.Print "round(1.5)=" & Round(1.5)
Debug.Print "round(1.6)=" & Round(1.6)
Debug.Print "round(2.5)=" & Round(2.5)
Debug.Print "round(2.6)=" & Round(2.6)
Debug.Print "round(3.5)=" & Round(3.5)
Debug.Print "round(3.6)=" & Round(3.6)
Debug.Print "round(4.5)=" & Round(4.5)
Debug.Print "round(4.6)=" & Round(4.6)
Debug.Print "round(1.15,1)=" & Round(1.15, 1)
Debug.Print "round(1.16,1)=" & Round(1.16, 1)
Debug.Print "round(1.25,1)=" & Round(1.25, 1)
Debug.Print "round(1.26,1)=" & Round(1.26, 1)
Debug.Print "round(1.35,1)=" & Round(1.35, 1)
Debug.Print "round(1.36,1)=" & Round(1.36, 1)
Debug.Print "round(1.45,1)=" & Round(1.45, 1)
Debug.Print "round(1.46,1)=" & Round(1.46, 1)
Debug.Print "Format(1.4,0)=" & Format(1.4, "0")
Debug.Print "Format(1.5,0)=" & Format(1.5, "0")
Debug.Print "Format(1.6,0)=" & Format(1.6, "0")
Debug.Print "Format(2.4,0)=" & Format(2.4, "0")
Debug.Print "Format(2.5,0)=" & Format(2.5, "0")
Debug.Print "Format(2.6,0)=" & Format(2.6, "0")
Debug.Print "Format(3.5,0)=" & Format(3.5, "0")
Debug.Print "Format(3.6,0)=" & Format(3.6, "0")
Debug.Print "Format(4.5,0)=" & Format(4.5, "0")
Debug.Print "Format(4.6,0)=" & Format(4.6, "0")
Debug.Print "Format(1.14, 0.0)=" & Format(1.14, "0.0")
Debug.Print "Format(1.15, 0.0)=" & Format(1.15, "0.0")
Debug.Print "Format(1.16, 0.0)=" & Format(1.16, "0.0")
Debug.Print "Format(1.24, 0.0)=" & Format(1.24, "0.0")
Debug.Print "Format(1.25, 0.0)=" & Format(1.25, "0.0")
Debug.Print "Format(1.26, 0.0)=" & Format(1.26, "0.0")
Debug.Print "Format(1.35, 0.0)=" & Format(1.35, "0.0")
Debug.Print "Format(1.36, 0.0)=" & Format(1.36, "0.0")
Debug.Print "Format(1.45, 0.0)=" & Format(1.45, "0.0")
Debug.Print "Format(1.46, 0.0)=" & Format(1.46, "0.0")
關於format要用0還是#都可以,差別在於0在小數位會補0
ref:https://msdn.microsoft.com/en-us/library/wb216dct(v=vs.90).aspx
沒有留言:
張貼留言