Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swiftui specify double format

Text("Värde \(Double(calc2, specifier: "%.2f").rounded())") 

//ERROR

I am converting a Slider value that uses a double type, but i cant format specify it? I get an error and xcode tells me to use signaling(?). I've tried putting it into an Int as well.

whats the right move here?

like image 781
oscar Avatar asked Nov 14 '19 00:11

oscar


2 Answers

You can use

Text("Värde \(calc2, specifier: "%.2f")") 

to convert it to 2 decimals.

To have greater flexibility you can use formatter and leverage appendInterpolation on string. Use the below function or something to that effect


func customFormat(_ number: Double) -> String  {
   let customFormatter = NumberFormatter()
   customFormatter.roundingMode = .down
   customFormatter.maximumFractionDigits = 2

   return "\(number, formatter: customFormatter)"   
}

like image 183
Partha G Avatar answered Nov 27 '22 05:11

Partha G


I assume you want something like

Text("Värde \(String(format: "%.2f", calc2))")
like image 44
Asperi Avatar answered Nov 27 '22 03:11

Asperi