Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 15
Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 18
Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 21
Warning: Undefined array key 2 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 27
Warning: Undefined array key 3 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 30
Warning: Undefined array key 4 in /home/naoooooo8/naoya-ono.com/public_html/swift/wp-content/themes/sango-theme-poripu/library/functions/prp_content.php on line 33
多言語化対応のため、アプリに表示する金額(数字)を、ロケーションによって変更する方法を紹介します。
例えば、僕の住むフィリピンでアプリを利用すると、このように「ペソ」で表示され、しかも小数点第二位まで表示されるようになります。

実装方法
実装方法は次の通りです。
// 表示する数字
var totalPrice: Float = 1234.56
// 表示する数字のフォーマットを利用する国に合わせる
let formatter = NumberFormatter()
formatter.numberStyle = .currency // 通貨記号をつける
// UILabelにセット
totalPriceLabel.text = formatter.string(from: NSNumber(value: totalPrice))
これで、画面を確認すると、

ロケーションを日本にすると、四捨五入され整数で表示されます。

通貨記号が不要な場合
通貨の記号(アイコン)が不要な場合は、以下のコードを追加してください。
formatter.currencySymbol = "" // 通貨記号を消す
他の方法として、次のようにnumberStyleにnone(デフォルト値)を設定することもできますが、この場合は、全てintegerになるので数字の小数点が消えてしまいます。
formatter.numberStyle = .none
//例
123.45 → 123になる


