本文整理汇总了VB.NET中System.Math.Round方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Math.Round方法的具体用法?VB.NET Math.Round怎么用?VB.NET Math.Round使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Math
的用法示例。
在下文中一共展示了Math.Round方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
Module Example
Public Sub Main()
Dim values() As Decimal = { 1.15d, 1.25d, 1.35d, 1.45d, 1.55d, 1.65d }
Dim sum As Decimal
' Calculate true mean.
For Each value In values
sum += value
Next
Console.WriteLine("True mean: {0:N2}", sum/values.Length)
' Calculate mean with rounding away from zero.
sum = 0
For Each value In values
sum += Math.Round(value, 1, MidpointRounding.AwayFromZero)
Next
Console.WriteLine("AwayFromZero: {0:N2}", sum/values.Length)
' Calculate mean with rounding to nearest.
sum = 0
For Each value In values
sum += Math.Round(value, 1, MidpointRounding.ToEven)
Next
Console.WriteLine("ToEven: {0:N2}", sum/values.Length)
End Sub
End Module
输出:
True mean: 1.40 AwayFromZero: 1.45 ToEven: 1.40
示例2: Example
Module Example
Public Sub Main()
Dim value As Double = 11.1
Console.WriteLine("{0,5} {1,20:R} {2,12} {3,15}\n",
"Value", "Full Precision", "ToEven",
"AwayFromZero")
For ctr As Integer = 0 To 5
value = RoundValueAndAdd(value)
Next
Console.WriteLine()
value = 11.5
RoundValueAndAdd(value)
End Sub
Private Function RoundValueAndAdd(value As Double) As Double
Const tolerance As Double = 8e-14
Console.WriteLine("{0,5:N1} {0,20:R} {1,12} {2,15}",
value,
RoundApproximate(value, 0, tolerance, MidpointRounding.ToEven),
RoundApproximate(value, 0, tolerance, MidpointRounding.AwayFromZero))
Return value + .1
End Function
Private Function RoundApproximate(dbl As Double, digits As Integer, margin As Double,
mode As MidpointRounding) As Double
Dim fraction As Double = dbl * Math.Pow(10, digits)
Dim value As Double = Math.Truncate(fraction)
fraction = fraction - value
If fraction = 0 Then Return dbl
Dim tolerance As Double = margin * dbl
' Determine whether this is a midpoint value.
If (fraction >= .5 - tolerance) And (fraction <= .5 + tolerance) Then
If mode = MidpointRounding.AwayFromZero Then
Return (value + 1)/Math.Pow(10, digits)
Else
If value Mod 2 <> 0 Then
Return (value + 1)/Math.Pow(10, digits)
Else
Return value/Math.Pow(10, digits)
End If
End If
End If
' Any remaining fractional value greater than .5 is not a midpoint value.
If fraction > .5 Then
Return (value + 1)/Math.Pow(10, digits)
Else
return value/Math.Pow(10, digits)
End If
End Function
End Module
输出:
Value Full Precision ToEven AwayFromZero 11.1 11.1 11 11 11.2 11.2 11 11 11.3 11.299999999999999 11 11 11.4 11.399999999999999 11 11 11.5 11.499999999999998 12 12 11.6 11.599999999999998 12 12 11.5 11.5 12 12
示例3: Example
Module Example
Public Sub Main()
Dim value As Single = 16.325
Console.WriteLine("Widening Conversion of {0:R} (type {1}) to {2:R} (type {3}): ",
value, value.GetType().Name, CDbl(value),
CDbl(value).GetType().Name)
Console.WriteLine(Math.Round(value, 2))
Console.WriteLine(Math.Round(value, 2, MidpointRounding.AwayFromZero))
Console.WriteLine()
Dim decValue As Decimal = CDec(value)
Console.WriteLine("Cast of {0:R} (type {1}) to {2} (type {3}): ",
value, value.GetType().Name, decValue,
decValue.GetType().Name)
Console.WriteLine(Math.Round(decValue, 2))
Console.WriteLine(Math.Round(decValue, 2, MidpointRounding.AwayFromZero))
Console.WriteLine()
End Sub
End Module
输出:
Widening Conversion of 16.325 (type Single) to 16.325000762939453 (type Double): 16.33 16.33 Cast of 16.325 (type Single) to 16.325 (type Decimal): 16.32 16.33
示例4: Sample
' This example demonstrates the Math.Round() method in conjunction
' with the MidpointRounding enumeration.
Class Sample
Public Shared Sub Main()
Dim result As Decimal = 0D
Dim posValue As Decimal = 3.45D
Dim negValue As Decimal = -3.45D
' By default, round a positive and a negative value to the nearest even number.
' The precision of the result is 1 decimal place.
result = Math.Round(posValue, 1)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue)
result = Math.Round(negValue, 1)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue)
Console.WriteLine()
' Round a positive value to the nearest even number, then to the nearest number
' away from zero. The precision of the result is 1 decimal place.
result = Math.Round(posValue, 1, MidpointRounding.ToEven)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", _
result, posValue)
result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", _
result, posValue)
Console.WriteLine()
' Round a negative value to the nearest even number, then to the nearest number
' away from zero. The precision of the result is 1 decimal place.
result = Math.Round(negValue, 1, MidpointRounding.ToEven)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)", _
result, negValue)
result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)", _
result, negValue)
Console.WriteLine()
End Sub
End Class
输出:
3.4 = Math.Round( 3.45, 1) -3.4 = Math.Round(-3.45, 1) 3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven) 3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero) -3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven) -3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
示例5: Example
Module Example
Public Sub Main()
Dim posValue As Double = 3.45
Dim negValue As Double = -3.45
' Round a positive and a negative value using the default.
Dim result As Double = Math.Round(posValue, 1)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, posValue)
result = Math.Round(negValue, 1)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1)", result, negValue)
Console.WriteLine()
' Round a positive value using a MidpointRounding value.
result = Math.Round(posValue, 1, MidpointRounding.ToEven)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)",
result, posValue)
result = Math.Round(posValue, 1, MidpointRounding.AwayFromZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)",
result, posValue)
Console.WriteLine()
' Round a positive value using a MidpointRounding value.
result = Math.Round(negValue, 1, MidpointRounding.ToEven)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.ToEven)",
result, negValue)
result = Math.Round(negValue, 1, MidpointRounding.AwayFromZero)
Console.WriteLine("{0,4} = Math.Round({1,5}, 1, MidpointRounding.AwayFromZero)",
result, negValue)
Console.WriteLine()
End Sub
End Module
输出:
3.4 = Math.Round( 3.45, 1) -3.4 = Math.Round(-3.45, 1) 3.4 = Math.Round( 3.45, 1, MidpointRounding.ToEven) 3.5 = Math.Round( 3.45, 1, MidpointRounding.AwayFromZero) -3.4 = Math.Round(-3.45, 1, MidpointRounding.ToEven) -3.5 = Math.Round(-3.45, 1, MidpointRounding.AwayFromZero)
示例6: Example
Module Example
Public Sub Main()
Dim values() As Double = { 2.125, 2.135, 2.145, 3.125, 3.135, 3.145 }
For Each value As Double In values
Console.WriteLine("{0} --> {1}", value,
Math.Round(value, 2, MidpointRounding.AwayFromZero))
Next
End Sub
End Module
输出:
2.125 --> 2.13 2.135 --> 2.13 2.145 --> 2.15 3.125 --> 3.13 3.135 --> 3.14 3.145 --> 3.15
示例7: Example
Module Example
Public Sub Main()
Dim values() As Double = { 12.0, 12.1, 12.2, 12.3, 12.4, 12.5, 12.6,
12.7, 12.8, 12.9, 13.0 }
Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default",
"ToEven", "AwayFromZero")
For Each value In values
Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}",
value, Math.Round(value),
Math.Round(value, MidpointRounding.ToEven),
Math.Round(value, MidpointRounding.AwayFromZero))
Next
End Sub
End Module
输出:
Value Default ToEven AwayFromZero 12 12 12 12 12.1 12 12 12 12.2 12 12 12 12.3 12 12 12 12.4 12 12 12 12.5 12 12 13 12.6 13 13 13 12.7 13 13 13 12.8 13 13 13 12.9 13 13 13 13.0 13 13 13
示例8: Example
Module Example
Public Sub Main()
Dim value As Double = 11.1
For ctr As Integer = 0 To 5
value = RoundValueAndAdd(value)
Next
Console.WriteLine()
value = 11.5
RoundValueAndAdd(value)
End Sub
Private Function RoundValueAndAdd(value As Double) As Double
Console.WriteLine("{0} --> {1}", value, Math.Round(value,
MidpointRounding.AwayFromZero))
Return value + .1
End Function
End Module
输出:
11.1 --> 11 11.2 --> 11 11.3 --> 11 11.4 --> 11 11.5 --> 11 11.6 --> 12 11.5 --> 12
示例9:
Math.Round(3.44, 1) 'Returns 3.4.
Math.Round(3.45, 1) 'Returns 3.4.
Math.Round(3.46, 1) 'Returns 3.5.
Math.Round(4.34, 1) ' Returns 4.3
Math.Round(4.35, 1) ' Returns 4.4
Math.Round(4.36, 1) ' Returns 4.4
示例10: Example
Module Example
Public Sub Main()
Dim values() As Double = { 2.125, 2.135, 2.145, 3.125, 3.135, 3.145 }
For Each value As Double In values
Console.WriteLine("{0} --> {1}", value, Math.Round(value, 2))
Next
End Sub
End Module
输出:
2.125 --> 2.12 2.135 --> 2.13 2.145 --> 2.14 3.125 --> 3.12 3.135 --> 3.14 3.145 --> 3.14
示例11: Module1
Module Module1
Sub Main()
Console.WriteLine("Classic Math.Round in Visual Basic")
Console.WriteLine(Math.Round(4.4)) ' 4
Console.WriteLine(Math.Round(4.5)) ' 4
Console.WriteLine(Math.Round(4.6)) ' 5
Console.WriteLine(Math.Round(5.5)) ' 6
End Sub
End Module
示例12: Example
Module Example
Public Sub Main()
Dim value As Double = 11.1
For ctr As Integer = 0 To 5
value = RoundValueAndAdd(value)
Next
Console.WriteLine()
value = 11.5
RoundValueAndAdd(value)
End Sub
Private Function RoundValueAndAdd(value As Double) As Double
Console.WriteLine("{0} --> {1}", value, Math.Round(value))
Return value + .1
End Function
End Module
输出:
11.1 --> 11 11.2 --> 11 11.3 --> 11 11.4 --> 11 11.5 --> 11 11.6 --> 12 11.5 --> 12
示例13: Example
Public Module Example
Sub Main()
Console.WriteLine(Math.Round(3.44, 1))
Console.WriteLine(Math.Round(3.45, 1))
Console.WriteLine(Math.Round(3.46, 1))
Console.WriteLine()
Console.WriteLine(Math.Round(4.34, 1))
Console.WriteLine(Math.Round(4.35, 1))
Console.WriteLine(Math.Round(4.36, 1))
End Sub
End Module
输出:
3.4 3.4 3.5 4.3 4.4 4.4
示例14: Example
Module Example
Public Sub Main()
For value As Decimal = 4.2d To 4.8d Step .1d
Console.WriteLine("{0} --> {1}", value, Math.Round(value))
Next
End Sub
End Module
输出:
4.2 --> 4 4.3 --> 4 4.4 --> 4 4.5 --> 4 4.6 --> 5 4.7 --> 5 4.8 --> 5
示例15: Example
Module Example
Public Sub Main()
Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}", "Value", "Default",
"ToEven", "AwayFromZero")
For value As Decimal = 12.0d To 13.0d Step .1d
Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-15}",
value, Math.Round(value),
Math.Round(value, MidpointRounding.ToEven),
Math.Round(value, MidpointRounding.AwayFromZero))
Next
End Sub
End Module
输出:
Value Default ToEven AwayFromZero 12 12 12 12 12.1 12 12 12 12.2 12 12 12 12.3 12 12 12 12.4 12 12 12 12.5 12 12 13 12.6 13 13 13 12.7 13 13 13 12.8 13 13 13 12.9 13 13 13 13.0 13 13 13