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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/262555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:23:18  来源:igfitidea点击:

Defining different types of numbers in C#

c#numbers

提问by Sekhat

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

您可以在 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?

我个人正在寻找哪个short,但是为了使问题成为人们更好的参考,您可以应用的所有其他数字文字的后修复是什么?

回答by Gulzar Nazim

for money:

为了钱:

decimal mon = 1m;

for output:

对于输出:

string curr = String.Format("{0:C}", mon);  //output .00

回答by Skizz

Integer

整数

Suffix - Description

后缀 - 描述

none - first of int, uint, long and ulong

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

U or u - first of uint, ulong

U 或 u - uint 中的第一个,ulong

L or l - first of long, ulong

L 或 l - 第一个长,ulong

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

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

Real

真实的

Suffix - Description

后缀 - 描述

none - double

无双

F or f - float

F 或 f - 浮动

D or d - double

D 或 d - 双倍

M or m - decimal

M 或 m - 十进制

回答by Scott Dorman

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.

[1] 当整数文字没有后缀时,它的类型是可以表示其值的这些类型中的第一个: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.

当整数文字仅指定 U 或 u 后缀时,其类型是这些类型中第一个可以表示其值的类型: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.

当整数文字仅指定 L 或 l 后缀时,其类型是这些类型中第一个可以表示其值的类型: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.

当整数文字指定 U 或 u 和 L 或 l 后缀时,其类型是这些类型中第一个可以表示其值的类型:ulong。