22

You can define a number in various ways in C#,

1F // a float with the value 1
1L // a long with the value 1
1D // a double with the value 1

personally I'm looking for which would a short, however to make the question a better reference for people, what are all the other post-fix's to number literals you can apply?

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
Sekhat
  • 4,435
  • 6
  • 43
  • 50
  • 1
    There is no literal syntax for integral types with smaller range/capacity than `int`, you just create a variable of that type then assign an `int` to it (i.e. `short x = 10;`) – bdukes Mar 03 '11 at 19:34

3 Answers3

27
Type        Suffix    .NET Framework Type                  
-------------------------------------------------------------------------------------
decimal     M or m    System.Decimal
double      D or d    System.Double
float       F or f    System.Single
int         [1]       System.Int32
long        L or l    System.Int64

[1] When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong.

When an integer literal specifies only a U or u suffix, its type is the first of these types in which its value can be represnted: uint, ulong.

When an integer literal specifies only a L or l suffix, its type is the first of these types in which its value can be represnted: long, ulong.

When an integer literal specifies both a U or u and L or l suffix, its type is the first of these types in which its value can be represnted: ulong.

Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
  • Very good answer. Further answers can be found under this question: https://stackoverflow.com/q/5820721/3604523 – h.m.i.13 Mar 31 '23 at 09:59
3

Integer

Suffix - Description

none - first of int, uint, long and ulong

U or u - first of uint, ulong

L or l - first of long, ulong

UL, Ul, uL, ul, LU, Lu, lU, or lu - ulong

Real

Suffix - Description

none - double

F or f - float

D or d - double

M or m - decimal

Community
  • 1
  • 1
Skizz
  • 69,698
  • 10
  • 71
  • 108
2

for money:

decimal mon = 1m;

for output:

string curr = String.Format("{0:C}", mon);  //output $1.00
Gulzar Nazim
  • 51,744
  • 26
  • 128
  • 170