LoginSignup
2
0

More than 1 year has passed since last update.

swift5 playgroundで試した備忘録(0埋め、空白埋め、丸め処理、置換、カウントなど)

Last updated at Posted at 2022-03-10

Xcode環境:Version 13.2.1

command+Shift+Enterで実行ショートカット

地味に便利

playgroundで 「no member」 エラーが出る

private var text = "Programming is difficult to understand."
print(text.replacingOccurrences(of: "difficult", with: "fun"))
// エラー文
Value of type 'String' has no member 'replacingOccurrences'
// DeepL翻訳 -> 型 'String' の値は、メンバー 'replacingOccurrences' を持っていません。

型を持たせてあげるには? -> ライブラリをimportする 

// 先頭にインポートで解決
import SwiftUI
private var text = "Programming is difficult to understand."
print(text.replacingOccurrences(of: "difficult", with: "fun"))
// エラー解消

ゼロパディング・ゼロサムシング 

参考:swift – 数値の0埋め、空白埋め

// ゼロパディング(8桁の場合)
1234 -> 00001234
56.789 -> 0056.789 // 小数点も含めて8桁表示
// ゼロサムシング(8桁の場合)
1234 -> ␣␣␣␣1234  // ␣はスペースの代わり
56.789 -> ␣␣56.789 // 小数点も含めて8桁表示
// 【ゼロパディング】
print(String(format: "%08d", 1234)) // ゼロパディング 00001234
print(String(format: "%08d", 3.14)) // 小数点のゼロパディング失敗する 1374389535 
// 【ゼロサプレス】
print(String(format: "% 8d", 1234)) // ゼロサプレス    ␣␣␣␣1234
print(String(format: "% 8d", 3.14)) // 小数点のゼロサプレス失敗する  ␣1374389535
// 【四捨五入+ゼロパディング、ゼロサプレス】
// 小数第三位を残す四捨五入+ゼロパディング
print(String(format: "%010.3f", 3.14)) // 0003.140
// 小数第三位を残す四捨五入+ゼロサプレス
print(String(format: "% 10.3f", 3.14)) // ␣␣␣3.140

整数のときはd、小数のときはfを使う
詳細は参考サイトへ

Dobule型の切り捨て、切り上げ、四捨五入

参考:【Swift】小数の丸め処理(round, rounded)のまとめ
元の値を変えてもよい -> round()
元の値を変えたくない -> rounded()
色々な丸め方があるよ:詳細は参考サイトへ

private let doubleNum = 1234.5678901
// 【切り捨て】
print(floor(doubleNum))         // 小数点以下の切り捨て  1234.0
print(doubleNum.rounded(.down)) // 小数点以下の切り捨て  1234.0
print(floor(doubleNum*10)/10)   // 切り捨ての位置をずらす 1234.5
print(floor(doubleNum*100)/100) // 切り捨ての位置をずらす 1234.56
// 【切り上げ】
print(ceil(doubleNum))          // 小数点以下の切り上げ 1235.0
print(doubleNum.rounded(.up))   // 小数点以下の切り上げ 1235.0
print(ceil(doubleNum*10)/10)    // 切り上げ位置をずらす 1234.6
print(ceil(doubleNum*100)/100)  // 切り上げ位置をずらす 1234.57
// 【四捨五入】 
print(doubleNum.rounded())                         // 小数点以下の四捨五入 1235.0
print(doubleNum.rounded(.toNearestOrAwayFromZero)) // 小数点以下の四捨五入 1235.0

print(round(doubleNum))         // 小数点以下の四捨五入 1235.0  元のdoubleNumは1235.0に変わっている
print(round(doubleNum*10)/10)   // 四捨五入位置をずらす  1234.6  元のdoubleNumは1234.6に変わっている
print(round(doubleNum*100)/100) // 四捨五入位置をずらす  1234.57 元のdoubleNumは1234.57に変わっている

print(String(format: "%.1f", doubleNum)) // 小数点第一位を残す四捨五入 1234.6
print(String(format: "%.5f", doubleNum)) // 小数点第五位を残す四捨五入 1234.56789
// 【四捨五入+ゼロパディング、ゼロサプレス】
print(String(format: "%010.3f", doubleNum)) // 小数点のゼロパディング 001234.568
print(String(format: "% 10.3f", doubleNum)) // 小数点のゼロサプレス  ␣␣1234.568

乱数を生成したい

print(Int.random(in: 1...10))    // 1〜10の間で乱数を発生
print(Double.random(in: 1...10)) // 1.0〜10.0の間で乱数を発生

置換したい

参考:Swiftで文字列の長さをチェックするのにlengthを使ってみよう

private var text = "Programming is difficult to understand."
// 置換 ofをwithに変換する
print(text.replacingOccurrences(of: "difficult", with: "fun")) // Programming is fun to understand.

文字数カウント

private let text2 = "Programming is fun to understand."
print(text2.count)                        // 文字列の長さをカウント、空白も含む 33
print(text2.replacingOccurrences(of: " ", with: "").count) // 空白を除外 29

後々追記予定です。

2
0
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
2
0