[swift] 数字を3桁区切り+小数点第2位まで表示させる
数字を3桁区切り+小数点第2位まで表示させる
サンプルコード:
func numFormatter(num:Float)->String{ let formatter = NSNumberFormatter() formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle formatter.groupingSeparator = "," formatter.groupingSize = 3 //3桁に区切る formatter.maximumFractionDigits = 2 formatter.positiveFormat = "0.00" formatter.roundingMode = NSNumberFormatterRoundingMode.RoundHalfUp // 四捨五入 return formatter.stringFromNumber(num)! } let pi = 3.14159265359 let largePi = Float(pi*1000) let str = numFormatter(largePi) print(str) //3,141.59