VB.NET 中数字的小数位
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(7)
考虑整数值的类似方法。
A similar approach that accounts for integer values.
对于全球化...
For globalizations ...
这个问题附加的一些其他答案建议将数字转换为字符串,然后使用“点”的字符位置作为小数位数的指示符。但这并不是一个可靠的方法。如果数字有很多小数位,并且它转换为包含指数表示法的字符串,则会导致答案非常不准确。
例如,对于方程 1 / 11111111111111111(1 除以 17),字符串转换为“9E-17”,这意味着结果结果是 5,而实际上应该是 17。当然可以从当“E-”存在时,它是字符串的结尾,但是当它可以通过数学方式完成时,为什么要这样做呢?
这是我刚刚编写的一个函数来执行此操作。这不是一个完美的解决方案,我还没有彻底测试它,但它似乎有效。
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.
只需使用以下内置函数
Just use the below in-built functions
简单...其中 n 是响应的位数
:100.12
Simple...where n are the number of digits
response: 100.12