在 C# 中定义不同类型的数字

问题描述 投票:0回答:3

您可以在 C# 中通过多种方式定义数字,

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

我个人正在寻找哪个是

short
,但是为了让这个问题更好地为人们提供参考,您可以应用的所有其他后修复数字文字是什么?

c# numbers
3个回答
28
投票
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] 当整数文字没有后缀时,其类型是可以表示其值的第一个类型:int、

uint
long
ulong

当整数文字仅指定

U
u
后缀时,其类型是可以表示其值的第一个类型:
uint
ulong

当整数文字仅指定

L
l
后缀时,其类型是可以表示其值的第一个类型:
long
ulong

当整数文字同时指定

U
u
L
l
后缀时,其类型是可以表示其值的这些类型中的第一个:
ulong


3
投票

整数

后缀 - 描述

无 - int、uint、long 和 ulong 中的第一个

U 或 u - uint、ulong 的第一个

L 或 l - long、ulong 中的第一个

UL、UL、uL、ul、LU、Lu、lU 或 lu - ulong

真实

后缀 - 描述

无 - 双

F 或 f - 浮动

D 或 d - 双

M 或 m - 小数


2
投票

为了钱:

decimal mon = 1m;

输出:

string curr = String.Format("{0:C}", mon);  //output $1.00
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.