public static BigNumber Pow(BigNumber Num1,BigNumber Num2,int precision = 30) { //Num1为底,Num2为幂次 //precision默认为保留30位小数 if (Num2 > 0) { return Num1.Power(Num2, precision); } else if (Num2 <0) { Num2 = 0 - Num2; return 1 / Num1.Power(Num2, precision); } else { return 1; } }
public static string round(string number, int digits, int type) { //type为0时四舍五入,1为ground,2为ceiling int NumOriginLen = number.Length; char[] digit_dot = { '.' }; string[] NumberBroken; NumberBroken = number.Split(digit_dot); if (digits < 0) { digits = 0; } if (NumberBroken[0].Length == NumOriginLen) { if (digits <= 0) { return number; } else { return NumberBroken[0] + ".".PadRight(digits + 1, '0'); } } else { string decimal_part = " "; BigNumber zero_point_one = new BigNumber("0.1"); BigNumber one = new BigNumber("1"); if (NumberBroken[1].Length > digits) { if (type == 1) { decimal_part = NumberBroken[1].Substring(0, digits); } else if (type == 2) { BigNumber carry = new BigNumber(digits.ToString()); carry = zero_point_one.Power(carry, 200); BigNumber number_changed = new BigNumber(number); number_changed = number_changed + carry; if (digits <= 0) { return number_changed.ToString().Substring(0, NumberBroken[0].Length); } return number_changed.ToString().Substring(0, NumOriginLen - (NumberBroken[1].Length - digits)); } else { if (Convert.ToInt32(NumberBroken[1].Substring(digits, 1)) > 4) { BigNumber carry = new BigNumber(digits.ToString()); carry = zero_point_one.Power(carry, 200); BigNumber number_changed = new BigNumber(number); number_changed = number_changed + carry; return number_changed.ToString().Substring(0, NumOriginLen - (NumberBroken[1].Length - digits)); } else { decimal_part = NumberBroken[1].Substring(0, digits); } } } else { decimal_part = NumberBroken[1].PadRight(digits, '0'); } if (decimal_part == "") { return NumberBroken[0]; } return NumberBroken[0] + '.' + decimal_part; } }