NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

decimal

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 NGWS Type
decimal 1.0 × 10-28 to 7.9 × 1028 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, for example:

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.

For more information on explicit numeric conversions, see Explicit Numeric Conversions Table.

Example

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

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

Output

12.1

In the preceding example, if you attempt to add the double and decimal variables by using a statement like this:

Console.WriteLine(d + x);   // error

you will get the following error:

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

Formatting Decimal Output

You can format the results using the Format method of the runtime library of the NGWS Framework, as shown in the following example.

Example

In this example, the Format method is used to display the results. Notice that x is rounded because the decimal places exceed $0.99. y, which represents the maximum exact digits, is displayed exactly in the proper format.

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

Output

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

For more information on the Format method, see the Format Class. For more information on the decimal type precision, see §4.1.6 in the language reference.

See Also

C# Keywords | Default Values Table | Built-in Types Table | Floating-point Types Table | Implicit Numeric Conversions Table | Explicit Numeric Conversions Table