本文整理匯總了C#中System.Decimal結構體的典型用法代碼示例。如果您正苦於以下問題:C# Decimal結構體的具體用法?C# Decimal怎麽用?C# Decimal使用的例子?那麽, 這裏精選的結構體代碼示例或許可以為您提供幫助。
Decimal結構體屬於System命名空間,在下文中一共展示了Decimal結構體的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: AddPenny
/// <summary>
/// Keeping my fortune in Decimals to avoid the round-off errors.
/// </summary>
class PiggyBank {
protected decimal MyFortune;
public void AddPenny() {
MyFortune = Decimal.Add(MyFortune, .01m);
}
public decimal Capacity {
get {
return Decimal.MaxValue;
}
}
public decimal Dollars {
get {
return Decimal.Floor(MyFortune);
}
}
public decimal Cents {
get {
return Decimal.Subtract(MyFortune, Decimal.Floor(MyFortune));
}
}
public override string ToString() {
return MyFortune.ToString("C")+" in piggy bank";
}
}
示例2:
decimal dividend = Decimal.One;
decimal divisor = 3;
// The following displays 0.9999999999999999999999999999 to the console
Console.WriteLine(dividend/divisor * divisor);
示例3:
decimal dividend = Decimal.One;
decimal divisor = 3;
// The following displays 1.00 to the console
Console.WriteLine(Math.Round(dividend/divisor * divisor, 2));