共用方式為


decimal (C# Reference) 

The decimal keyword denotes a 128-bit data type. Compared to floating-point types, the decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations. The approximate range and precision for the decimal type are shown in the following table.

Type Approximate Range Precision .NET Framework type

decimal

±1.0 × 10e−28 to ±7.9 × 10e28

28-29 significant digits

System.Decimal

Literals

If you want a numeric real literal to be treated as decimal, use the suffix m or M, for example:

decimal myMoney = 300.5m;

Without the suffix m, the number is treated as a double, thus generating a compiler error.

Conversions

The integral types are implicitly converted to decimal and the result evaluates to decimal. Therefore you can initialize a decimal variable using an integer literal, without the suffix, as follows:

decimal myMoney = 300;

There is no implicit conversion between floating-point types and the decimal type; therefore, a cast must be used to convert between these two types. For example:

decimal myMoney = 99.9m;
double x = (double)myMoney;
myMoney = (decimal)x;

You can also mix decimal and numeric integral types in the same expression. However, mixing decimal and floating-point types without a cast results in a compilation error.

For more information on implicit numeric conversions, see Implicit Numeric Conversions Table (C# Reference).

For more information on explicit numeric conversions, see Explicit Numeric Conversions Table (C# Reference).

Formatting Decimal Output

You can format the results by using the String.Format method, or through the System.Console.Write method, which calls String.Format(). The currency format is specified using the standard currency format string "C" or "c," as shown in Example 2. For more information on the String.Format method, see System.String.Format(System.String,System.Object).

Example

In this example, a decimal and an int are mixed in the same expression. The result evaluates to the decimal type.

If you attempt to add the double and decimal variables by using a statement like this:

double x = 9;
Console.WriteLine(d + x); // Error

you will get the following error:

Operator '+' cannot be applied to operands of type 'double' and 'decimal'

// keyword_decimal.cs
// decimal conversion
using System;
public class TestDecimal 
{
    static void Main () 
    {
        decimal d = 9.1m;
        int y = 3;
        Console.WriteLine(d + y);   // Result converted to decimal
    }
}

Output

12.1

In this example, the output is formatted using the currency format string. Notice that x is rounded because the decimal places exceed $0.99. The variable y, which represents the maximum exact digits, is displayed exactly in the proper format.

// keyword_decimal2.cs
// Decimal type formatting
using System;
public class TestDecimalFormat 
{
    static void Main () 
    {
        decimal x = 0.999m;
        decimal y = 9999999999999999999999999999m;
        Console.WriteLine("My amount = {0:C}", x);
        Console.WriteLine("Your amount = {0:C}", y);
    }
}

Output

My amount = $1.00
Your amount = $9,999,999,999,999,999,999,999,999,999.00

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.3 Types and Variables

  • 4.1.7 The decimal Type

See Also

Reference

C# Keywords
Integral Types Table (C# Reference)
Built-In Types Table (C# Reference)
Implicit Numeric Conversions Table (C# Reference)
Explicit Numeric Conversions Table (C# Reference)
Decimal Structure

Concepts

C# Programming Guide

Other Resources

C# Reference