123_456_789 的类型是什么? [英] What is type of 123_456_789?

查看:23
本文介绍了123_456_789 的类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Swift 示例中看到像 123_456_789 这样的值,带有下划线的数字.这些值的默认类型是什么?

I see in Swift examples values like 123_456_789, numbers with underscores. What type do these values have by default?

这取决于我分配给它们的变量的类型吗?它们对我来说看起来很有趣而且很新,所以我想知道,如果它们像它们一样被抛出而没有定义类型,它们会被怎样对待?

Does it depend on the type of the variable I assign them to? They look quite funny and new to me, so I wonder, how are they treated if they are thrown just like they are, without defining a type?

推荐答案

来自文档(Swift 编程语言 -> 语言指南 -> 基础知识-> 数字文字):

From the documentation (The Swift Programming Language -> Language Guide -> The Basics -> Numeric Literals):

数字文字可以包含额外的格式以使其更容易读.整数和浮点数都可以用额外的零填充,并且可以包含下划线以帮助提高可读性.两种类型都没有格式会影响文字的基础值:

Numeric literals can contain extra formatting to make them easier to read. Both integers and floats can be padded with extra zeros and can contain underscores to help with readability. Neither type of formatting affects the underlying value of the literal:

let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1

所以你的 123_456_789 是一个整数文字,与 123456789 相同.您可以在任何地方插入下划线,而不仅仅是作为千位分隔符",例如 1_2_3_4_5_6_7_8_91_23_4567_89,如果您喜欢编写混淆代码.

So your 123_456_789 is a integer literal, and identical to 123456789. You can insert the underscores wherever you want, not only as a "thousands separator", such as 1_2_3_4_5_6_7_8_9 or 1_23_4567_89, if you like to write obfuscated code.

这篇关于123_456_789 的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆