[Swift] I want to do something like C's sprinftf

Thing you want to do

I want to display the specified value as a character string according to the specified format. In short, I want to do something like the following in C language.

sprintf.c


sprintf(str, "%.2f", pi)

Implementation method

There is a String type initializer that takes a format string as an argument, so use that.

stringformat.swift


let pi = 3.14159
let str = String(format: "%.2f", pi)
print(str) 

Output result


3.14

It can also be used when rounding off and displaying in integer notation.

stringformat2.swift


let height = 182.9
let str = String(format: "%.0f", height)
print(str)

Output result


183

You can also take a String type as an argument. In this case, use % @ as the format string.

stringformat3.swift


let s1 = "Hot"
let s2 = "Natsu"
let str = String(format: "%@Is%@I don't know.", , )

Output result


It's hot.

Operation check environment

Xcode: 11.7 iOS: 13.7 Swift version: Swift5

that's all

Recommended Posts

[Swift] I want to do something like C's sprinftf
I want to do something like "cls" in Java
Swift: I want to chain arrays
I want to do team development remotely
[Swift] I want to draw grid lines (squares)
[Ruby] I want to do a method jump!
Grouping [RxJava] [1,1,2,2,3,3,1,1] like [[1,1], [2,2], [3,3], [1,1]] To do
I want to use FireBase to display a timeline like Twitter
I want to create a chat screen for the Swift chat app!
I want to use FormObject well
I want to convert InputStream to String
I want to docker-compose up Next.js!
I really want to do "new T ()"! (And without inspection exceptions)
I want to remove the top margin in Grouped UITableView (swift)
[iOS] I tried to make a processing application like Instagram with Swift
I want to develop a web application!
I want to write a nice build.gradle
I want to eliminate duplicate error messages
I want to make an ios.android app
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
I want to write a unit test!
I want to install PHP 7.2 on Ubuntu 20.04.
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I tried to use Selenium like JQuery
I want to target static fields to @Autowired
I want to test Action Cable with RSpec test
I want to sort by tab delimited by ruby
I want to output the day of the week
Run R from Java I want to run rJava
I want to send an email in Java.
I want to graduate from npm install properly [2020]
I want to use arrow notation in Ruby
I want to var_dump the contents of the intent
I want to simply write a repeating string
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
I want to play with Firestore from Rails
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I want to reduce simple mistakes. To command yourself.
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to delete files managed by Git
I want to get the value in Ruby