Reputation: 4829
I need to convert small number to a BigInteger type but it results zero. consider following code:
BigInteger x = new BigInteger(0.6);
var res = BigInteger.Pow(x, 10) / Factorial(30);
in the first line 0.6 conversion to BigInteger results zero and the entire code return wrong result. any idea?
Upvotes: 1
Views: 150
Reputation: 1995
The integer part of 0.6
is 0
so the result is correct.
if you want use Math.Round()
to convert to the rounded integer which in your case is 1
.
BigInteger x = new BigInteger(Math.Round(0.6));
Upvotes: 1
Reputation:
From: http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx
"Floating-point values are truncated before they are assigned to the BigInteger." Hence, see the following code snippet (from source):
BigInteger bigIntFromDouble = new BigInteger(179032.6541); Console.WriteLine(bigIntFromDouble); // The example displays the following output: // 179032
As a result, your BigInteger x = new BigInteger(0.6);
results zero because of the truncate...which is expected behavior.
Upvotes: 0