本文整理汇总了C#中BigDecimal.Divide方法的典型用法代码示例。如果您正苦于以下问题:C# BigDecimal.Divide方法的具体用法?C# BigDecimal.Divide怎么用?C# BigDecimal.Divide使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigDecimal
的用法示例。
在下文中一共展示了BigDecimal.Divide方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DivideRoundHalfUpNeg2
public void DivideRoundHalfUpNeg2()
{
String a = "-37361671119238118911893939591735";
int aScale = 10;
String b = "74723342238476237823787879183470";
int bScale = 15;
String c = "-1E+5";
int resScale = -5;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, resScale, RoundingMode.HalfUp);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(resScale, result.Scale, "incorrect scale");
}
示例2: DivideRoundHalfEvenPos
public void DivideRoundHalfEvenPos()
{
String a = "92948782094488478231212478987482988429808779810457634781384756794987";
int aScale = -24;
String b = "7472334223847623782375469293018787918347987234564568";
int bScale = 13;
String c = "1.24390557635720517122423359799284E+53";
int resScale = -21;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, resScale, RoundingMode.HalfEven);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(resScale, result.Scale, "incorrect scale");
}
示例3: DivideRoundHalfUpNeg1
public void DivideRoundHalfUpNeg1()
{
String a = "-92948782094488478231212478987482988798104576347813847567949855464535634534563456";
int aScale = -24;
String b = "74723342238476237823754692930187879183479";
int bScale = 13;
String c = "-1.2439055763572051712242335979928354832010167729111113605E+76";
int resScale = -21;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, resScale, RoundingMode.HalfUp);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(resScale, result.Scale, "incorrect scale");
}
示例4: DivideExpLessZero
public void DivideExpLessZero()
{
// Divide: local variable exponent is less than zero
const string a = "1231212478987482988429808779810457634781384756794987";
const int aScale = 15;
const string b = "747233429293018787918347987234564568";
const int bScale = 10;
string c = "1.64770E+10";
const int resScale = -5;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, resScale, RoundingMode.Ceiling);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(resScale, result.Scale, "incorrect scale");
}
示例5: DivideRemainderIsZero
public void DivideRemainderIsZero()
{
String a = "8311389578904553209874735431110";
int aScale = -15;
String b = "237468273682987234567849583746";
int bScale = 20;
String c = "3.5000000000000000000000000000000E+36";
int resScale = -5;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, resScale, RoundingMode.Ceiling);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(resScale, result.Scale, "incorrect scale");
}
示例6: DivideRound
public static BigDecimal DivideRound(BigDecimal x, int n)
{
// The estimation of the relative error in the result is |err(x)/x|
var mc = new MathContext(x.Precision);
return x.Divide(new BigDecimal(n), mc);
}
示例7: DivideBigDecimalI
public void DivideBigDecimalI()
{
BigDecimal divd1 = new BigDecimal(value, 2);
BigDecimal divd2 = BigDecimal.Parse("2.335");
BigDecimal divd3 = divd1.Divide(divd2, RoundingMode.Up);
Assert.IsTrue(divd3.ToString().Equals("52873.27") && divd3.Scale == divd1.Scale, "123459.08/2.335 is not correct");
Assert.IsTrue(divd3.UnscaledValue.ToString().Equals("5287327"),
"the unscaledValue representation of 123459.08/2.335 is not correct");
divd2 = new BigDecimal(123.4D);
divd3 = divd1.Divide(divd2, RoundingMode.Down);
Assert.IsTrue(divd3.ToString().Equals("1000.47") && divd3.Scale == 2, "123459.08/123.4 is not correct");
divd2 = new BigDecimal(000D);
Assert.Throws<ArithmeticException>(() => divd1.Divide(divd2, RoundingMode.Down));
}
示例8: DivideBigDecimalScaleRoundingModeUp
public void DivideBigDecimalScaleRoundingModeUp()
{
String a = "-37361671119238118911893939591735";
int aScale = 10;
String b = "74723342238476237823787879183470";
int bScale = -15;
int newScale = 31;
RoundingMode rm = RoundingMode.Up;
String c = "-5.00000E-26";
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, newScale, rm);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(newScale, result.Scale, "incorrect scale");
}
示例9: DivideByZero
public void DivideByZero()
{
String a = "1231212478987482988429808779810457634781384756794987";
int aScale = 15;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = BigDecimal.ValueOf(0L);
Assert.Throws<ArithmeticException>(() => aNumber.Divide(bNumber), "ArithmeticException has not been caught");
}
示例10: DivideBigDecimalScaleRoundingModeHalfEven
public void DivideBigDecimalScaleRoundingModeHalfEven()
{
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = 5;
String b = "74723342238476237823787879183470";
int bScale = 15;
int newScale = 7;
RoundingMode rm = RoundingMode.HalfEven;
String c = "500002603731642864013619132621009722.1803810";
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, newScale, rm);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(newScale, result.Scale, "incorrect scale");
}
示例11: DivideBigDecimalScaleRoundingModeHalfUp
public void DivideBigDecimalScaleRoundingModeHalfUp()
{
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = -51;
String b = "74723342238476237823787879183470";
int bScale = 45;
int newScale = 3;
RoundingMode rm = RoundingMode.HalfUp;
String c = "50000260373164286401361913262100972218038099522752460421" +
"05959924024355721031761947728703598332749334086415670525" +
"3761096961.670";
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, newScale, rm);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(newScale, result.Scale, "incorrect scale");
}
示例12: DivideBigDecimalScaleRoundingModeFloor
public void DivideBigDecimalScaleRoundingModeFloor()
{
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = 100;
String b = "74723342238476237823787879183470";
int bScale = 15;
int newScale = 45;
RoundingMode rm = RoundingMode.Floor;
String c = "0E-45";
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, newScale, rm);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(newScale, result.Scale, "incorrect scale");
}
示例13: DivideBigDecimalScaleMathContextUp
public void DivideBigDecimalScaleMathContextUp()
{
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = 15;
String b = "748766876876723342238476237823787879183470";
int bScale = 10;
int precision = 21;
RoundingMode rm = RoundingMode.Up;
MathContext mc = new MathContext(precision, rm);
String c = "49897861180.2562512996";
int resScale = 10;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, mc);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(resScale, result.Scale, "incorrect scale");
}
示例14: DivideBigDecimalScaleMathContextHALF_UP
public void DivideBigDecimalScaleMathContextHALF_UP()
{
String a = "3736186567876876578956958765675671119238118911893939591735";
int aScale = 45;
String b = "134432345432345748766876876723342238476237823787879183470";
int bScale = 70;
int precision = 21;
RoundingMode rm = RoundingMode.HalfUp;
MathContext mc = new MathContext(precision, rm);
String c = "2.77923185514690367475E+26";
int resScale = -6;
BigDecimal aNumber = new BigDecimal(BigInteger.Parse(a), aScale);
BigDecimal bNumber = new BigDecimal(BigInteger.Parse(b), bScale);
BigDecimal result = aNumber.Divide(bNumber, mc);
Assert.AreEqual(c, result.ToString(), "incorrect value");
Assert.AreEqual(resScale, result.Scale, "incorrect scale");
}
示例15: Sqrt
public static BigDecimal Sqrt(BigDecimal x, MathContext mc)
{
if (x.CompareTo(BigDecimal.Zero) < 0)
throw new ArithmeticException("negative argument " + x + " of square root");
if (x.Abs().Subtract(new BigDecimal(System.Math.Pow(10d, -mc.Precision))).CompareTo(BigDecimal.Zero) < 0)
return ScalePrecision(BigDecimal.Zero, mc);
/* start the computation from a double precision estimate */
var s = new BigDecimal(System.Math.Sqrt(x.ToDouble()), mc);
BigDecimal half = BigDecimal.ValueOf(2);
/* increase the local accuracy by 2 digits */
var locmc = new MathContext(mc.Precision + 2, mc.RoundingMode);
/* relative accuracy requested is 10^(-precision)
*/
double eps = System.Math.Pow(10.0, -mc.Precision);
while (true) {
/* s = s -(s/2-x/2s); test correction s-x/s for being
* smaller than the precision requested. The relative correction is 1-x/s^2,
* (actually half of this, which we use for a little bit of additional protection).
*/
if (System.Math.Abs(BigDecimal.One.Subtract(x.Divide(s.Pow(2, locmc), locmc)).ToDouble()) < eps)
break;
s = s.Add(x.Divide(s, locmc)).Divide(half, locmc);
}
return s;
}