Private Sub Command1_Click()Dim num1!'這里定義位single而不是整型!Dim num2!If Text1.Text <> "" And Text2.Text <> "" Thennum1 = Val(Text1.Text)num2 = Val(Text2.Text)Text1.Text = Int(num1 * 10 ^ num2 + 0.5) / 10 ^ num2ElseMsgBox ("請(qǐng)輸入所有的數(shù)值")End IfEnd Sub
2、VB中有專門用來四舍五入的函數(shù)Round 函數(shù):返回按指定位數(shù)進(jìn)行四舍五入的數(shù)值。語法:Round(expression[, numdecimalplaces])參數(shù):expression(必選項(xiàng))-被四舍五入的數(shù)值表達(dá)式 ;numdecimalplaces(可選項(xiàng))-數(shù)字表明小數(shù)點(diǎn)右邊有多少位進(jìn)行四舍五入,如果省略則 Round 函數(shù)返回整數(shù)。下面的示例利用 Round 函數(shù)將數(shù)值四舍五入到兩位小數(shù): Dim MyVar, pipi = 3.14159MyVar = Round(pi, 2) 'MyVar contains 3.14。
聯(lián)系客服