2015. 12. 6.

스위프트에서 세자리마다 쉼표찍기( Show comma on every 3 digit in swift -Number formatting)

//: Playground - noun: a place where people can play

import UIKit

func convertCurrency( money : NSNumber, style : NSNumberFormatterStyle ) -> String {
    
    let numberFormatter = NSNumberFormatter()
    numberFormatter.numberStyle = style
    
    return numberFormatter.stringFromNumber( money )!
}

let value = 12345678.123

convertCurrency(value, style : NSNumberFormatterStyle.DecimalStyle)
convertCurrency(value, style : NSNumberFormatterStyle.NoStyle)
convertCurrency(value, style : NSNumberFormatterStyle.CurrencyStyle)

#=========== Output  ===================
12,345,678.123     <-- decimalstyle="" font="">
12345678             <-- font="" nostyle="">
$12,345,678.12     <-- currencystyle="" font="">