2013-03-05 19 views
1

我需要將小數轉換爲BigInteger類型,但結果爲零。在第一線0.6轉換在C#中將小數轉換爲BigInteger類型

BigInteger x = new BigInteger(0.6); 
var res = BigInteger.Pow(x, 10)/Factorial(30); 

到BigInteger的結果爲零,整個代碼返回錯誤的結果: 考慮下面的代碼。 有什麼想法?

+0

如果'Factorial'返回'BigInteger'你應該做'BigInteger.Pow(X,10).Divide(階乘(30))'。就第一行而言,你是否嘗試過'新的BigInteger(Convert.toDouble(「0.6」))',看看它是否有效?我很好奇。 – nattyddubbs 2013-03-05 14:04:10

回答

0

來源:http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx

「他們被分配到的BigInteger前浮點值將被截斷。」因此,請參見下面的代碼片段(從源代碼):

BigInteger bigIntFromDouble = new BigInteger(179032.6541); 
Console.WriteLine(bigIntFromDouble); 
// The example displays the following output: 
// 179032 

其結果是,你BigInteger x = new BigInteger(0.6);的結果,因爲截斷......這是預期的行爲的零。

1

0.6的整數部分是0所以結果是正確的。

如果你想使用Math.Round()轉換爲四捨五入整數,在你的情況是1

BigInteger x = new BigInteger(Math.Round(0.6));