【问题标题】:Prevent Math.Round(95.55555555,2) from rounding to 95.56 in VB.NET防止 Math.Round(95.55555555,2) 在 VB.NET 中舍入到 95.56
【发布时间】:2011-03-30 15:59:59
【问题描述】:

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

【问题讨论】:

  • 问题是:将它舍入到 95.55 而不是 95.56 的精确条件是什么?我认为,如果只是在哪个点舍入到较小而不是较大的限制问题,那么编写自己的舍入函数是微不足道的。我不会尝试,因为我不是很喜欢 VB... :-) 但要小心边框效果...
  • 您希望Math.Round(95.55666666,2) 舍入到 95.55 还是 95.56?你实际上是四舍五入还是只是在 x 精度后将其截断?
  • 听起来你实际上并不想要一个回合,而是一个截断。
  • 是的,我基本上想截断,但保留两位小数。

标签: vb.net


【解决方案1】:

看起来像Math.Truncate(95.5555555 * 100) / 100
Truncate Decimal number not Round Off

【解决方案2】:

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

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

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

【讨论】:

  • 你在说什么? Floor(95.5555555... * 100) = 95559555/100 = 95.55
  • 我的一个问题是,如果我想截断到小数点后 3 位怎么办,这将如何改变 Math.Floor 或 Math.Truncate 方法。例如,我希望 95.5556 为 95.555。
  • 看了一个类似的问题后,是不是只是在做类似 Math.Floor(value * 1000) / 1000;保留 3 位小数。
  • 是的,这正是您要保留 3 位小数的方式。
【解决方案3】:

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

一个更好的方法可能是

Math.Truncate(number * 100) / 100

这只是将数字乘以 100 并截断它,为您提供一个包含所需数字的整数值,然后除以 100 将其恢复为小数。

【讨论】:

  • Math.Floor(number * 100) /100 和 Math.Truncate(number * 100) / 100 有什么区别?
  • Floor 用于向下取整。截断只是削减。天花板四舍五入
  • @Xaisoft:Floor 总是向下取整,所以如果您的值为负数,您会看到差异。
  • 在这种情况下,它们是返回相同结果的两个不同操作。 Floor() 返回小于输入的最接近的整数值(但不一定是Integer 类型),而Truncate() 会去掉小数点后的所有内容。 它们只是处理负数的方式不同。
【解决方案4】:

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

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

您的结果将是 95.55。

【讨论】:

    【解决方案5】:

    你可以用这个:

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

    对不起,这是 C#,但我想你可以很容易猜到如何翻译成 vb。

    【讨论】:

      【解决方案6】:
      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
      

      【讨论】:

        猜你喜欢
        • 2014-06-15
        • 1970-01-01
        • 2015-07-15
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 1970-01-01
        • 1970-01-01
        • 2010-10-16
        相关资源
        最近更新 更多