56
54

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift]数字を三桁ごとにカンマ区切りにする

Posted at

よくお金の表記なんかで使う奴ですね。
2,048
こういうやつ。

let int = 2048
var num = NSNumber(integer: int)

var formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
formatter.groupingSeparator = ","
formatter.groupingSize = 3
        
var result = formatter.stringFromNumber(num)

書き方は特にObjective-Cと変わりませんでした。
強いて上げるならば
NSNumberFormatterDecimalStyle

NSNumberFormatterStyle.DecimalStyle
という書き方に変わっているところでしょう。

56
54
2

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
56
54

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?