C-formatting in Swift如何演变成一个长尾词的?

2026-04-16 23:241阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计311个文字,预计阅读时间需要2分钟。

C-formatting in Swift如何演变成一个长尾词的?

在Swift中格式化与C相似:创建一个包含格式化字符串的String类型变量或常量,然后使用print()函数显示变量或常量。例如:

swiftlet integerValue: Int=1let doubleValue: Double=2.33print(integerValue: \(integerValue), doubleValue: \(doubleValue))

C-formatting in Swift is similar to that in C: create a String type variable or constant with a c-formatting string, and display the variable or constant using the print() function.

let integerValue: Int = 1 let doubleValue: Double = 2.33 let characterValue: Character = "c" let stringValue: String = "str" let boolValue: Bool = true let characterValueStr = String(characterValue) // (1) Character -> String let stringToPrint = String(format: "%d %.2f %@ %@", integerValue, doubleValue, characterValueStr, stringValue) print(stringToPrint)

result:

1 2.33 c str

Something to notice:
A Bool type value cannot be printed with "%b".

A Character type value cannot be printed with "%c". To print a Character type value, convert it into a String type value using the String() initializer, see (1) above.

A String type value can be printed with "%@", not "%s".

Actually we can use the string interpolation in Swift together with the c-formatting, which also enables us to display the Character and Bool type value easily.

let doubleValueStr = String(format: "%.2f", doubleValue) print("\(integerValue) \(doubleValueStr) \(characterValue) \(stringValue) \(boolValue)")

result:

1 2.33 c str true

references:
(1) Swift:字符串格式化
(2) Swift - 数字格式化转成字符串(保留两位小数)
(3) 输出格式化

C-formatting in Swift如何演变成一个长尾词的?

本文共计311个文字,预计阅读时间需要2分钟。

C-formatting in Swift如何演变成一个长尾词的?

在Swift中格式化与C相似:创建一个包含格式化字符串的String类型变量或常量,然后使用print()函数显示变量或常量。例如:

swiftlet integerValue: Int=1let doubleValue: Double=2.33print(integerValue: \(integerValue), doubleValue: \(doubleValue))

C-formatting in Swift is similar to that in C: create a String type variable or constant with a c-formatting string, and display the variable or constant using the print() function.

let integerValue: Int = 1 let doubleValue: Double = 2.33 let characterValue: Character = "c" let stringValue: String = "str" let boolValue: Bool = true let characterValueStr = String(characterValue) // (1) Character -> String let stringToPrint = String(format: "%d %.2f %@ %@", integerValue, doubleValue, characterValueStr, stringValue) print(stringToPrint)

result:

1 2.33 c str

Something to notice:
A Bool type value cannot be printed with "%b".

A Character type value cannot be printed with "%c". To print a Character type value, convert it into a String type value using the String() initializer, see (1) above.

A String type value can be printed with "%@", not "%s".

Actually we can use the string interpolation in Swift together with the c-formatting, which also enables us to display the Character and Bool type value easily.

let doubleValueStr = String(format: "%.2f", doubleValue) print("\(integerValue) \(doubleValueStr) \(characterValue) \(stringValue) \(boolValue)")

result:

1 2.33 c str true

references:
(1) Swift:字符串格式化
(2) Swift - 数字格式化转成字符串(保留两位小数)
(3) 输出格式化

C-formatting in Swift如何演变成一个长尾词的?