0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Swift】よく使われる String の format で打線組んでみた

Last updated at Posted at 2024-05-21

打線に並び替えるとかだるいのでやめました。

String(format: "%02x", 255)
// ff

String(format: "%02X", 170)
// AA

String(format: "%02d", 1)
// 01

String(format: "% 5d", 2)
//     2

String(format: "%.2f", 1.2)
// 1.20

String(format: "%.2f", 1.204)
// 1.20

String(format: "%.2f", 1.205)
// 1.21

String(format: "%.0f", 1.23)
// 1

String(format: "%f", 3.12)
// 3.120000

String(format: "%+.2f", -123.5)
// -123.50

String(format: "%p", 3)
// 0x3

String(format: "%@", "x")
// x

String(format: "%2$@ %1$@", "world", "Hello")
// Hello world

四捨五入できることに驚いたので。

🤔 参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?