FormatNumber用0替换数字

FormatNumber用0替换数字

本文介绍了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:

然后我对此进行了测试:

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替换数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:44