Home > Software design >  Convert a double value to pt-BR real - Swift ios
Convert a double value to pt-BR real - Swift ios

Time:12-23

I have a Double, and i need show in label numbers in currency format, pt-BR format.

private var myValue: Double = 0.0
myLabel.text = "\(self.myValue)"

** i need convert this Double to pt-BR real **

i try this:

   let convert = self.myValue as NSNumber
    
   myLabel.text = "\(convert)"

CodePudding user response:

You should use NumberFormatter for this task. Here is an example:

Playground view:

enter image description here

let currency = 10.50

let formatter: NumberFormatter = {
    let result = NumberFormatter()
    result.locale = Locale(identifier: "pt-BR")
    result.numberStyle = .currency
    
    return result
}()

let label = UILabel()
label.text = formatter.string(for: currency)
  • Related