C# 短/长/int 文字格式?

发布于 2024-11-03 21:14:10 字数 319 浏览 1 评论 0原文

在 C/C#/等中。您可以告诉编译器,文字数字并不是它看起来的那样(即,float 而不是 doubleunsigned long 而不是int):

var d = 1.0;  // double
var f = 1.0f; // float
var u = 1UL;  // unsigned long

等等。

有人能给我列出这些内容吗?我专门寻找 shortInt16 的后缀。

In C/C#/etc. you can tell the compiler that a literal number is not what it appears to be (ie., float instead of double, unsigned long instead of int):

var d = 1.0;  // double
var f = 1.0f; // float
var u = 1UL;  // unsigned long

etc.

Could someone point me to a list of these? I'm specifically looking for a suffix for short or Int16.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不寐倦长更 2024-11-10 21:14:10
var d  = 1.0d;  // double
var d0 = 1.0;   // double
var d1 = 1e+3;  // double
var d2 = 1e-3;  // double
var f  = 1.0f;  // float
var m  = 1.0m;  // decimal
var i  = 1;     // int
var ui = 1U;    // uint
var ul = 1UL;   // ulong
var l  = 1L;    // long

我认为这就是全部......short/ushort/byte/sbyte 没有文字说明符

var d  = 1.0d;  // double
var d0 = 1.0;   // double
var d1 = 1e+3;  // double
var d2 = 1e-3;  // double
var f  = 1.0f;  // float
var m  = 1.0m;  // decimal
var i  = 1;     // int
var ui = 1U;    // uint
var ul = 1UL;   // ulong
var l  = 1L;    // long

I think that's all... there are no literal specifiers for short/ushort/byte/sbyte

属性 2024-11-10 21:14:10

来自 整数文字

整数文字的类型确定如下:

  • 如果文字没有后缀,则它具有可表示其值的第一种类型:intuintlong, ulong.
  • 如果文字以 Uu 为后缀,则它具有可以表示其值的第一种类型:uintulong
  • 如果文字以 Ll 为后缀,则它具有可以表示其值的第一种类型:longulong
  • 如果文字后缀为 ULUluLulLU< /code>、LulUlu,其类型为 ulong


并来自 真正的文字

如果未指定实数类型后缀,则实数文字的类型为 double。否则,真实类型后缀决定真实文字的类型,如下所示:

  • Ff 为后缀的实数文字的类型为 float。例如,文字 1f1.5f1e10f123.456F<​​/code> 都是 类型浮动
  • Dd 为后缀的实数文字的类型为 double。例如,文字 1d1.5d1e10d123.456D 都是 类型双
  • Mm 为后缀的实数文字的类型为 decimal。例如,文字 1m1.5m1e10m123.456M 都是 类型十进制。该文字通过获取精确值转换为十进制值,并在必要时使用银行四舍五入(第 4.1.7 节)四舍五入到最接近的可表示值。文字中出现的任何比例都会被保留,除非该值被舍入或该值为零(在后一种情况下,符号和比例将为 0)。因此,文字 2.900m 将被解析为符号 0、系数 2900 和小数位 3 的小数>.


From Integer literals:

The type of an integer literal is determined as follows:

  • If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.
  • If the literal is suffixed by U or u, it has the first of these types in which its value can be represented: uint, ulong.
  • If the literal is suffixed by L or l, it has the first of these types in which its value can be represented: long, ulong.
  • If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong.

And from Real literals:

If no real type suffix is specified, the type of the real literal is double. Otherwise, the real type suffix determines the type of the real literal, as follows:

  • A real literal suffixed by F or f is of type float. For example, the literals 1f, 1.5f, 1e10f, and 123.456F are all of type float.
  • A real literal suffixed by D or d is of type double. For example, the literals 1d, 1.5d, 1e10d, and 123.456D are all of type double.
  • A real literal suffixed by M or m is of type decimal. For example, the literals 1m, 1.5m, 1e10m, and 123.456M are all of type decimal. This literal is converted to a decimal value by taking the exact value, and, if necessary, rounding to the nearest representable value using banker's rounding (Section 4.1.7). Any scale apparent in the literal is preserved unless the value is rounded or the value is zero (in which latter case the sign and scale will be 0). Hence, the literal 2.900m will be parsed to form the decimal with sign 0, coefficient 2900, and scale 3.
〃安静 2024-11-10 21:14:10

如果你的变量还不是short,你必须显式地转换它:

Object s = (Int16) 1;

If your variable isn't already a short, you have to cast it explicitly :

Object s = (Int16) 1;
剧终人散尽 2024-11-10 21:14:10

没有一个可以简称。只需使用short s = 1;

There isn't one for short. Just use short s = 1;.

一直在等你来 2024-11-10 21:14:10
var myValue = unchecked((short)0x7F00);

文字是 int,因此必须转换为目标类型。
如果发生值溢出则需要不加检查。

var myValue = unchecked((short)0x7F00);

The literal is int and thus must be cast to target type.
If a value overflow occurs unchecked is required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文