2013-07-06 113 views
0

我正在將應用程序從VB6移植到C#。我發現一個特別的計算導致了我的問題。它基本上可以歸結爲混淆計算

opperandA *.01/opperandB 

我具體的例子是:

1 * .01/12 

在VB6(和Windows計算器),我得到8.3333333333e-4。

但是,在C#(以及其他所有計算器)中,我獲得.00083333。

第二個數字對我很有意義,但是我必須複製第一個結果並且我想理解它,那麼爲什麼VB6和Windows計算器會產生奇怪的結果?

+0

提示:[*科學記數法*](http://en.wikipedia.org/wiki/Scientific_notation)。使用'result.ToString(「R」)'將float/double轉換爲沒有這種表示法的字符串。 – Paul

回答

5

8.3333333333e-4與0.00083333相同。它等同於:

8.3333333333 * 10^-4 
= 8.3333333333 times (ten to the power of -4) 
= 8.3333333333 * 0.0001 
= 0.00083333333 
N.b. After rounding 

電子代表指數及相關的維基百科文章是http://en.wikipedia.org/wiki/Exponentiation

+0

然而,8.3333333333e-4比.00083333更準確。 VB可能默認爲浮動。試試opperandA * .01f/opperandB –

+0

@Matthew很好的解釋。 – bansi