FormatNumber用0替换数字 [英] FormatNumber replacing number with 0

查看:99
本文介绍了FormatNumber用0替换数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不理解这一点:$ b​​ $ b从DataReader返回的数字:185549633.66000035

Not understanding this: Number returned from DataReader: 185549633.66000035

我们要求保持每个用户选择的小数位数。

We have a requirement to maintain the number of decimal places per a User Choice.

例如:维护7个地方。

我们正在使用:

FormatNumber(dr.Item("Field"), 7, TriState.false, , TriState.True)

结果是:185,549,633.6600000。
我们想在最后保留3(或35)。

The result is: 185,549,633.6600000. We would like to maintain the 3 (or 35) at the end.

从结果查询中减去两个数字时,我们得到一个增量,但试图将这两个数字显示为6、7、8个数字不起作用,从而向用户指示错误的增量。

When subtracting two numbers from the resulting query we are getting a delta but trying to show these two numbers out to 6,7,8 digits is not working thus indicating a false delta to the user.

任何建议将不胜感激。

推荐答案

基于我的测试,您必须使用 Double 值而不是十进制。毫不奇怪,可以在文档中找到解决您问题的方法。

Based on my testing, you must be working with Double values rather than Decimal. Not surprisingly, the solution to your problem can be found in the documentation.

首先,您不应该使用 FormatNumber 。我们不再使用VB6 ToTo。要在VB.NET中格式化数字,请对该数字调用 ToString 。我对此进行了测试:

For a start, you should not be using FormatNumber. We're not in VB6 anymore ToTo. To format a number in VB.NET, call ToString on that number. I tested this:

Dim dbl = 185549633.66000035R
Dim dec = 185549633.66000035D

Dim dblString = dbl.ToString("n7")
Dim decString = dec.ToString("n7")

Console.WriteLine(dblString)
Console.WriteLine(decString)

我看到了您描述的行为,即输出为:

and I saw the behaviour you describe, i.e. the output was:


185,549,633.6600000
185,549,633.6600004

我阅读了 Double.ToString 方法(请注意, FormatNumber 会在内部调用 ToString ),这就是它的意思:

I read the documentation for the Double.ToString method (note that FormatNumber would be calling ToString internally) and this is what it says:


默认情况下,返回值仅包含15位精度,尽管最多保留17位数字内部。如果此实例的值大于15位数字,则ToString返回PositiveInfinitySymbol或NegativeInfinitySymbol而不是预期的数字。如果需要更高的精度,请使用 G17格式规范指定格式,该格式规范始终返回17位精度,如果使用该精度表示数字,则始终返回17位精度;如果仅以数字精度表示,则返回17位精度。

By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.

然后我对此进行了测试:

I then tested this:

Dim dbl = 185549633.66000035R

Dim dblString16 = dbl.ToString("G16")
Dim dblString17 = dbl.ToString("G17")

Console.WriteLine(dblString16)
Console.WriteLine(dblString17)

结果是:


185549633.6600004
185549633.66000035

这篇关于FormatNumber用0替换数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆