這顯然不起作用。從兩個大整數中獲取精確的百分比
BigInteger Total = 1000000000000000000000000000000000000000000000000000022234235423534543;
BigInteger Actual = 83450348250384508349058934085;
string Percent = ((Decimal)100.0/Total*Actual).ToString()+"%";
問題是,如何得到我的精確百分比?
目前我使用..
string sTotal = (task.End - task.Start).ToString();
BigInteger current = task.End;
string sCurrent = (task.End-current).ToString().PadLeft(sTotal.Length, '0');
Int32 maxLength = sCurrent.Length;
if (maxLength > Int64.MaxValue.ToString().Length - 1)
maxLength = Int64.MaxValue.ToString().Length - 1;
UInt64 currentI = Convert.ToUInt64(sCurrent.Substring(0, maxLength));
UInt64 totalI = Convert.ToUInt64(sTotal.Substring(0, maxLength));
Percent = (Decimal)100.0/totalI
* currentI;
您能否提供更好的?
你想讓它精確到幾個地方? –
我希望你可以使用一些浮點類型而不是BigInteger(有一些額外的魔法)。 – HopeNick
@EricLippert 56位數或更多 – Chris