本文整理汇总了C#中BigDecimal.SetScale方法的典型用法代码示例。如果您正苦于以下问题:C# BigDecimal.SetScale方法的具体用法?C# BigDecimal.SetScale怎么用?C# BigDecimal.SetScale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BigDecimal
的用法示例。
在下文中一共展示了BigDecimal.SetScale方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFormatSize
/// <summary>
/// 格式化显示大小
/// </summary>
/// <returns>The format size.</returns>
/// <param name="size">Size.</param>
public static string GetFormatSize(double size)
{
double kiloByte = size / 1024;
if (kiloByte < 1) {
return size + "Byte";
}
double megaByte = kiloByte / 1024;
if (megaByte < 1) {
BigDecimal result1 = new BigDecimal(kiloByte.ToString());
return result1.SetScale (2, RoundOptions.HalfUp).ToPlainString()+"KB";
}
double gigaByte = megaByte / 1024;
if (gigaByte < 1) {
BigDecimal result2 = new BigDecimal(megaByte.ToString());
return result2.SetScale(2,RoundOptions.HalfUp)
.ToPlainString() + "MB";
}
double teraBytes = gigaByte / 1024;
if (teraBytes < 1) {
BigDecimal result3 = new BigDecimal(gigaByte.ToString());
return result3.SetScale(2, RoundOptions.HalfUp)
.ToPlainString() + "GB";
}
BigDecimal result4 = new BigDecimal(teraBytes);
return result4.SetScale(2, RoundOptions.HalfUp).ToPlainString()
+ "TB";
}
示例2: SetScaleII
public void SetScaleII()
{
BigDecimal setScale1 = new BigDecimal(2.323E102);
BigDecimal setScale2 = setScale1.SetScale(4);
Assert.IsTrue(setScale2.Scale == 4, "the number 2.323E102 after setting scale is wrong");
Assert.IsTrue(setScale2.ToDouble() == 2.323E102, "the representation of the number 2.323E102 is wrong");
setScale1 = BigDecimal.Parse("-1.253E-12");
setScale2 = setScale1.SetScale(17, RoundingMode.Ceiling);
Assert.IsTrue(setScale2.Scale == 17, "the number -1.253E-12 after setting scale is wrong");
Assert.IsTrue(setScale2.ToString().Equals("-1.25300E-12"),
"the representation of the number -1.253E-12 after setting scale is wrong, " + setScale2);
// testing rounding Mode ROUND_CEILING
setScale1 = new BigDecimal(value, 4);
setScale2 = setScale1.SetScale(1, RoundingMode.Ceiling);
Assert.IsTrue(setScale2.ToString().Equals("1234.6") && setScale2.Scale == 1,
"the number 1234.5908 after setting scale to 1/ROUND_CEILING is wrong");
BigDecimal setNeg = new BigDecimal(value.Negate(), 4);
setScale2 = setNeg.SetScale(1, RoundingMode.Ceiling);
Assert.IsTrue(setScale2.ToString().Equals("-1234.5") && setScale2.Scale == 1,
"the number -1234.5908 after setting scale to 1/ROUND_CEILING is wrong");
// testing rounding Mode ROUND_DOWN
setScale2 = setNeg.SetScale(1, RoundingMode.Down);
Assert.IsTrue(setScale2.ToString().Equals("-1234.5") && setScale2.Scale == 1,
"the number -1234.5908 after setting scale to 1/ROUND_DOWN is wrong");
setScale1 = new BigDecimal(value, 4);
setScale2 = setScale1.SetScale(1, RoundingMode.Down);
Assert.IsTrue(setScale2.ToString().Equals("1234.5") && setScale2.Scale == 1,
"the number 1234.5908 after setting scale to 1/ROUND_DOWN is wrong");
// testing rounding Mode ROUND_FLOOR
setScale2 = setScale1.SetScale(1, RoundingMode.Floor);
Assert.IsTrue(setScale2.ToString().Equals("1234.5") && setScale2.Scale == 1,
"the number 1234.5908 after setting scale to 1/ROUND_FLOOR is wrong");
setScale2 = setNeg.SetScale(1, RoundingMode.Floor);
Assert.IsTrue(setScale2.ToString().Equals("-1234.6") && setScale2.Scale == 1,
"the number -1234.5908 after setting scale to 1/ROUND_FLOOR is wrong");
// testing rounding Mode ROUND_HALF_DOWN
setScale2 = setScale1.SetScale(3, RoundingMode.HalfDown);
Assert.IsTrue(setScale2.ToString().Equals("1234.591") && setScale2.Scale == 3,
"the number 1234.5908 after setting scale to 3/ROUND_HALF_DOWN is wrong");
setScale1 = new BigDecimal(BigInteger.Parse("12345000"), 5);
setScale2 = setScale1.SetScale(1, RoundingMode.HalfDown);
Assert.IsTrue(setScale2.ToString().Equals("123.4") && setScale2.Scale == 1,
"the number 123.45908 after setting scale to 1/ROUND_HALF_DOWN is wrong");
setScale2 = BigDecimal.Parse("-1234.5000").SetScale(0, RoundingMode.HalfDown);
Assert.IsTrue(setScale2.ToString().Equals("-1234") && setScale2.Scale == 0,
"the number -1234.5908 after setting scale to 0/ROUND_HALF_DOWN is wrong");
// testing rounding Mode ROUND_HALF_EVEN
setScale1 = new BigDecimal(1.2345789D);
setScale2 = setScale1.SetScale(4, RoundingMode.HalfEven);
Assert.IsTrue(setScale2.ToDouble() == 1.2346D && setScale2.Scale == 4,
"the number 1.2345789 after setting scale to 4/ROUND_HALF_EVEN is wrong");
setNeg = new BigDecimal(-1.2335789D);
setScale2 = setNeg.SetScale(2, RoundingMode.HalfEven);
Assert.IsTrue(setScale2.ToDouble() == -1.23D && setScale2.Scale == 2,
"the number -1.2335789 after setting scale to 2/ROUND_HALF_EVEN is wrong");
setScale2 = BigDecimal.Parse("1.2345000").SetScale(3,
RoundingMode.HalfEven);
Assert.IsTrue(setScale2.ToDouble() == 1.234D && setScale2.Scale == 3,
"the number 1.2345789 after setting scale to 3/ROUND_HALF_EVEN is wrong");
setScale2 = BigDecimal.Parse("-1.2345000").SetScale(3,
RoundingMode.HalfEven);
Assert.IsTrue(setScale2.ToDouble() == -1.234D && setScale2.Scale == 3,
"the number -1.2335789 after setting scale to 3/ROUND_HALF_EVEN is wrong");
// testing rounding Mode ROUND_HALF_UP
setScale1 = BigDecimal.Parse("134567.34650");
setScale2 = setScale1.SetScale(3, RoundingMode.HalfUp);
Assert.IsTrue(setScale2.ToString().Equals("134567.347") && setScale2.Scale == 3,
"the number 134567.34658 after setting scale to 3/ROUND_HALF_UP is wrong");
setNeg = BigDecimal.Parse("-1234.4567");
setScale2 = setNeg.SetScale(0, RoundingMode.HalfUp);
Assert.IsTrue(setScale2.ToString().Equals("-1234") && setScale2.Scale == 0,
"the number -1234.4567 after setting scale to 0/ROUND_HALF_UP is wrong");
Assert.Throws<ArithmeticException>(() => setScale1.SetScale(3, RoundingMode.Unnecessary));
// testing rounding Mode ROUND_UP
setScale1 = BigDecimal.Parse("100000.374");
setScale2 = setScale1.SetScale(2, RoundingMode.Up);
Assert.IsTrue(setScale2.ToString().Equals("100000.38") && setScale2.Scale == 2,
"the number 100000.374 after setting scale to 2/ROUND_UP is wrong");
setNeg = new BigDecimal(-134.34589D);
setScale2 = setNeg.SetScale(2, RoundingMode.Up);
Assert.IsTrue(setScale2.ToDouble() == -134.35D && setScale2.Scale == 2,
"the number -134.34589 after setting scale to 2/ROUND_UP is wrong");
Assert.Throws<ArgumentException>(() => setScale1.SetScale(0, -123));
}
示例3: ScalePrecision
public static BigDecimal ScalePrecision(BigDecimal x, int d)
{
return x.SetScale(d + x.Scale);
}
示例4: SetScaleI
public void SetScaleI()
{
// rounding mode defaults to zero
BigDecimal setScale1 = new BigDecimal(value, 3);
BigDecimal setScale2 = setScale1.SetScale(5);
BigInteger setresult = BigInteger.Parse("1234590800");
Assert.IsTrue(setScale2.UnscaledValue.Equals(setresult) && setScale2.Scale == 5,
"the number 12345.908 after setting scale is wrong");
Assert.Throws<ArithmeticException>(() => setScale1.SetScale(2, RoundingMode.Unnecessary));
}