Real Literals
Syntax:
<decimal-digits>.<decimal-digits>[exponent-part][real-type-suffix] .<decimal-digits>[exponent-part][real-type-suffix] <decimal-digits><exponent-part>[real-type-suffix] <decimal-digits><real-type-suffix>
Exponent-part:
e|E[+|-]<decimal-digits>
- The literals can be used in either line of the syntax.
- If no suffix is specified, the default type of the literal is double.
- F or f suffix can be used in float literals, for example, 1.5e3F.
- D or d suffix can be used in double literals, for example, .3456E-5D.
- M or m suffix can be used in decimal literals, for example, 7e-8M.
![]() |
---|
Hexadecimal is not allowed to be used in float literals, double literals or decimal literals. |
![]() |
---|
If the literal is outside of the range of the indicated type, a compile-time error will occur. |
See the following real literal examples and the comments.
3.78915 // Legal, no suffix so it is double type 12345E-3F // Legal, so it is a float literal 321E // Illegal, the exponent part is incomplete 210f // Legal, check the last line of the syntax .e55 // Illegal, missing fraction part, the digits following dot is a must 77e3 // Legal, check the third line of the syntax
Example 01-19-01:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System; namespace TestRealLiterals { class Program { static void Main(string[] args) { decimal m = 1234E1M; double d = 5678E-3D; //float f = 3.14; float f = 3.14f; Console.WriteLine("f={0}", f); Console.WriteLine("m+d={0}", (double)m+d); Console.WriteLine("m+d={0}", m+(decimal)d); Console.Read(); } } }
Output
f=3.14 m+d=12345.678 m+d=12345.678
- Line 9-10: A decimal and a double variable are defined and assigned.
- Line 11: If this line is not commented out, you will get a "Literal of type double cannot be implicitly converted to type 'float'" compile error. Because the default literal value without suffix owns a double type.
- Line 12: That's correct assignment by adding a suffix at the end of the literal.
- Line 14: Output the float variable value.
- Line 15-16: Output the addition result. You'll have to cast explicitly to the same type then do the addition.