防止 Math.Round(95.55555555,2) 在 VB.NET 中四舍五入到 95.56

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5488850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:23:26  来源:igfitidea点击:

Prevent Math.Round(95.55555555,2) from rounding to 95.56 in VB.NET

vb.net

提问by Xaisoft

If I do Math.Round(95.55555555,2)in VB.NET, the result is 95.56, but I want it the result the be 95.55. Is there a way to do this in VB.NET? I guess I just want to keep the decimal places, but not round them

如果我Math.Round(95.55555555,2)在 VB.NET 中这样做,结果是95.56,但我希望它的结果是95.55. 有没有办法在 VB.NET 中做到这一点?我想我只想保留小数位,而不是四舍五入

回答by Michael Blake

Looks like Math.Truncate(95.5555555 * 100) / 100.
See Truncate Decimal number not Round Off

看起来像Math.Truncate(95.5555555 * 100) / 100
请参阅截断十进制数而不是四舍五入

回答by Rob

Try using Math.Floor(95.55555555 * 100) / 100

尝试使用 Math.Floor(95.55555555 * 100) / 100

Or, if you want to round to a specific number of decimals:

或者,如果您想四舍五入到特定的小数位数:

Public Function RoundDown(ByVal value As Double, ByVal decimalPlaces As Integer) As Double
    If (decimalPlaces < 1) Then
        Throw New ArgumentException("Invalid decimalPlaces: less than 1")
    EndIf

    Dim factor As Integer = 10 ^ decimalPlaces
    Return Math.Floor(value * factor) / factor
End Sub

回答by Bill the Lizard

There are several ways to do this. One would be to subtract 0.05 from the number then use Math.Round(number, 2). (This works on the same principle as implementing floorand ceilingfunctions when all you have is round.)

有几种方法可以做到这一点。一种方法是从数字中减去 0.05,然后使用Math.Round(number, 2). (这与实现floorceiling功能的原理相同,当您只有round.)

A better way is probably

更好的方法可能是

Math.Truncate(number * 100) / 100

That just multiplies the number by 100 and truncates it, giving you an integer value with the digits you want, then divides by 100 to turn it back to a decimal.

这只是将数字乘以 100 并截断它,为您提供一个包含您想要的数字的整数值,然后除以 100 将其转换回小数。

回答by Geo Ego

You don't want Math.Round. You want Math.Truncate.

你不想要 Math.Round。你想要 Math.Truncate。

Dim decimalNumber As Double = 95.55555555
Dim truncatedNumber As Double = Math.Truncate(decimalNumber * 100) / 100

Your result will be 95.55.

您的结果将是 95.55。

回答by Felice Pollano

You can use this:

你可以使用这个:

static double TruncateWithDecimals(double n, int nOfDec)
        {
           return Math.Round(Math.Floor(n * Math.Pow(10, nOfDec)) /  Math.Pow(10, nOfDec), nOfDec);
        }

sorry this is C#, but you can easily guess how to translate in vb I think.

抱歉,这是 C#,但我认为您可以轻松猜出如何在 vb 中进行翻译。

回答by Paul Ishak

Public Function Round(Number As Decimal, places As Integer) As Decimal
    'Convert number to string
    Dim NumberString As String = Number.ToString
    'Check if the number contains a decimal, if not return the number
    If NumberString.IndexOf("."c) = -1 Then Return Number
    'Get the whole number part of the string
    Dim IntegerPart As String = NumberString.Split("."c)(0)
    'Get the Decimal part of the string
    Dim DecimalPart As String = NumberString.Split("."c)(1)
    'If the number is already rounded to n decimal places, then return the number
    If DecimalPart.Length = places Then Return Number
    'Get whichever decimals are being rounded to
    Dim ToPlacePart As String = DecimalPart.Substring(0, places)
    'get the other part that will be compared
    Dim ComparePart As String = DecimalPart.Substring(places)
    'Create a midpoint to compare the compare part to
    Dim ControlMidPoint As Decimal = Decimal.Parse("1" & Replace(Space(ComparePart.Length), Space(1), "0")) / 2
    'Create the base result(Add the integer part to the decimal part that will stay)
    Dim Result As Decimal = Decimal.Parse(IntegerPart & "." & ToPlacePart)
    'Create an increment to add if the comparepart is greater than the mid point(ex 0.001, 0.01, 0.0000001)
    Dim AddNum As Decimal = Decimal.Parse("0." & Replace(Space(ToPlacePart.Count - 1), Space(1), "0") & "1")
    'If the comparepart was equal to or greater than the midpoint, then add the addpart to the base result and return it
    If Decimal.Parse(ComparePart) >= ControlMidPoint Then Return Result + AddNum
    'Just return the base result, because the compare part was smaller than the midpoint
    Return Result
End Function