Swift 3 Nsnumberformatter issue

Discussion in 'Mac Programming' started by abcdefg12345, Sep 9, 2016.

  1. abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    I just converter project to swift 3 and now I'm having errors with numberformatter

    i tried the following and they all don't work anymore

    Code:
    let result = 123.456
    let numberFormatter: NumberFormatter = NumberFormatter()
    numberFormatter.groupingSeparator = ","
    numberFormatter.groupingSize = 3
    numberFormatter.usesGroupingSeparator = true
    numberFormatter.decimalSeparator = "."
    numberFormatter.numberStyle = NumberFormatter.Style.decimal
    numberFormatter.minimumFractionDigits = 15
    let theString: String = numberFormatter.stringFromNumber((result))! // i get error cannot convert value of type double to expected argument type 'NSNumber'
    
    let theString: String = numberFormatter.stringFromNumber((NSNumber(result)))! // changed it to this and i get error "Argument labels '(_:)' do not match any available overloads"
    
    let theString = numberFormatter.stringFromNumber((NSNumber(result)))! // changed it to this with same error
    not sure what to do anymore, i looked everywhere and there are no guides for swift 3 number formatter
     
  2. MirekEl macrumors newbie

    MirekEl

    Joined:
    Dec 1, 2015
    #2
    If I recall correctly, the function is now called string, not stringWithNumber
     
  3. anheca68 macrumors newbie

    Joined:
    Apr 6, 2017
    #3
    --- Post Merged, Apr 6, 2017 ---
    con las modificaciones en swift 3.0 tu código debería de funcionar de la siguiente manera ..... slds
    let theString: String = numberFormatter.string(from: (NSNumber(value: result)))!
     
  4. spiffers macrumors member

    Joined:
    Apr 12, 2009
    #4
    Code:
    let result: NSNumber = 123.456 // Set the varible to be of type NSNumber
    let numberFormatter: NumberFormatter = NumberFormatter()
    numberFormatter.groupingSeparator = ","
    numberFormatter.groupingSize = 3
    numberFormatter.usesGroupingSeparator = true
    numberFormatter.decimalSeparator = "."
    numberFormatter.numberStyle = NumberFormatter.Style.decimal
    numberFormatter.minimumFractionDigits = 15
    
    let theString: String = numberFormatter.string(from: result)! // And here you use the new API, numberFormatter.string(from:)
     

Share This Page