decimal 类型。
decimal 变量:
double x = 9;
Console.WriteLine(d + x); // Error
其结果是导致以下错误:
Operator '+' cannot be applied to operands of type 'double' and 'decimal'
-
public class TestDecimal
- {
-
static void Main()
- {
-
decimal d = 9.1m;
-
int y = 3;
- Console.WriteLine(d + y); // Result converted to decimal
- }
- }
- // Output: 12.1
-
y 严格按照正确的格式显示。
-
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
- */
-