VB.NET 中数字的小数位

发布于 2024-12-12 10:12:38 字数 101 浏览 6 评论 0原文

在 VB.NET 中如何检查数字有多少位小数?

例如:在循环内,我有一个 if 语句,在该语句中我想检查一个数字是否有四位小数 (8.9659)。

How do I check how many decimal places a number has in VB.NET?

For example: Inside a loop I have an if statement and in that statement I want to check if a number has four decimal places (8.9659).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

奈何桥上唱咆哮 2024-12-19 10:12:38

考虑整数值的类似方法。

Public Function NumberOfDecimalPlaces(ByVal number As Double) As Integer
    Dim numberAsString As String = number.ToString()
    Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".")

    If indexOfDecimalPoint = -1 Then ' No decimal point in number
        Return 0
    Else
        Return numberAsString.Substring(indexOfDecimalPoint + 1).Length
    End If

End Function

A similar approach that accounts for integer values.

Public Function NumberOfDecimalPlaces(ByVal number As Double) As Integer
    Dim numberAsString As String = number.ToString()
    Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".")

    If indexOfDecimalPoint = -1 Then ' No decimal point in number
        Return 0
    Else
        Return numberAsString.Substring(indexOfDecimalPoint + 1).Length
    End If

End Function
琉璃梦幻 2024-12-19 10:12:38
Dim numberAsString As String = myNumber.ToString()
Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".")
Dim numberOfDecimals As Integer = _
    numberAsString.Substring(indexOfDecimalPoint + 1).Length
Dim numberAsString As String = myNumber.ToString()
Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".")
Dim numberOfDecimals As Integer = _
    numberAsString.Substring(indexOfDecimalPoint + 1).Length
枕花眠 2024-12-19 10:12:38
  Public Shared Function IsInSignificantDigits(val As Double, sigDigits As Integer)
    Dim intVal As Double = val * 10 ^ sigDigits
    Return intVal = Int(intVal)
   End Function
  Public Shared Function IsInSignificantDigits(val As Double, sigDigits As Integer)
    Dim intVal As Double = val * 10 ^ sigDigits
    Return intVal = Int(intVal)
   End Function
和我恋爱吧 2024-12-19 10:12:38

对于全球化...

Public Function NumberOfDecimalPlaces(ByVal number As Double) As Integer
    Dim numberAsString As String = number.ToString(System.Globalization.CultureInfo.InvariantCulture)
    Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".")

    If (indexOfDecimalPoint = -1) Then ' No decimal point in number
        Return 0
    Else
        Return numberAsString.Substring(indexOfDecimalPoint + 1).Length
    End If

End Function

For globalizations ...

Public Function NumberOfDecimalPlaces(ByVal number As Double) As Integer
    Dim numberAsString As String = number.ToString(System.Globalization.CultureInfo.InvariantCulture)
    Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".")

    If (indexOfDecimalPoint = -1) Then ' No decimal point in number
        Return 0
    Else
        Return numberAsString.Substring(indexOfDecimalPoint + 1).Length
    End If

End Function
还在原地等你 2024-12-19 10:12:38

这个问题附加的一些其他答案建议将数字转换为字符串,然后使用“点”的字符位置作为小数位数的指示符。但这并不是一个可靠的方法。如果数字有很多小数位,并且它转换为包含指数表示法的字符串,则会导致答案非常不准确。

例如,对于方程 1 / 11111111111111111(1 除以 17),字符串转换为“9E-17”,这意味着结果结果是 5,而实际上应该是 17。当然可以从当“E-”存在时,它是字符串的结尾,但是当它可以通过数学方式完成时,为什么要这样做呢?

这是我刚刚编写的一个函数来执行此操作。这不是一个完美的解决方案,我还没有彻底测试它,但它似乎有效。

Public Function CountOfDecimalPlaces(ByVal inputNumber As Variant) As Integer
'
' This function returns the count of deciml places in a number using simple math and a loop. The
' input variable is of the Variant data type, so this function is versatile enougfh to work with
' any type of input number.
'
CountOfDecimalPlaces = 0                            'assign a default value of zero
inputNumber = VBA.CDec(inputNumber)                 'convert to Decimal for more working space
inputNumber = inputNumber - VBA.Fix(inputNumber)    'discard the digits left of the decimal
Do While inputNumber <> VBA.Int(inputNumber)        'when input = Int(input), it's done
    CountOfDecimalPlaces = CountOfDecimalPlaces + 1 'do the counting
    inputNumber = inputNumber * 10                  'move the decimal one place to the right
Loop                                                'repeat until no decimal places left
End Function

Some of the other answers attached to this question suggest converting the number to a string and then using the character position of the "dot" as the indicator of the number of decimal places. But this isn't a reliable way to do it & would result in wildly inaccurate answers if the number had many decimal places, and its conversion to a string contained exponential notation.

For instance, for the equation 1 / 11111111111111111 (one divided by 17 ones), the string conversion is "9E-17", which means the resulting answer is 5 when it should be 17. One could of course extract the correct answer from the end of the string when the "E-" is present, but why do all that when it could be done mathematically instead?

Here is a function I've just cooked up to do this. This isn't a perfect solution, and I haven't tested it thoroughly, but it seems to work.

Public Function CountOfDecimalPlaces(ByVal inputNumber As Variant) As Integer
'
' This function returns the count of deciml places in a number using simple math and a loop. The
' input variable is of the Variant data type, so this function is versatile enougfh to work with
' any type of input number.
'
CountOfDecimalPlaces = 0                            'assign a default value of zero
inputNumber = VBA.CDec(inputNumber)                 'convert to Decimal for more working space
inputNumber = inputNumber - VBA.Fix(inputNumber)    'discard the digits left of the decimal
Do While inputNumber <> VBA.Int(inputNumber)        'when input = Int(input), it's done
    CountOfDecimalPlaces = CountOfDecimalPlaces + 1 'do the counting
    inputNumber = inputNumber * 10                  'move the decimal one place to the right
Loop                                                'repeat until no decimal places left
End Function
旧城烟雨 2024-12-19 10:12:38

只需使用以下内置函数

Dim DecimalLength = StrReverse(number.ToString).IndexOf(".")
MsgBox(DecimalLength)

Just use the below in-built functions

Dim DecimalLength = StrReverse(number.ToString).IndexOf(".")
MsgBox(DecimalLength)
霊感 2024-12-19 10:12:38

简单...其中 n 是响应的位数

Dim n as integer = 2
Dim d as decimal = 100.123456
d = Math.Round(d, n);
MessageBox.Show(d.ToString())

:100.12

Simple...where n are the number of digits

Dim n as integer = 2
Dim d as decimal = 100.123456
d = Math.Round(d, n);
MessageBox.Show(d.ToString())

response: 100.12

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文