ANOTHER EXAMPLE OF SWIFTUI GARBAGE!

well, you can trim a decimal or float variable down to 2 decimal places.

Tried using .round-->nope doesn't work

tried using NumberFormatter-->nope doesnt work.

tried NSFormat-->nope doesn't work.


Just another frustraing example of why I HATE SWIFTUI

Accepted Reply

struct ContentView: View {
    let a = 12.3456
    var body: some View {
        Text("\(a, specifier: "%.2f")")
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

This works on preview, simulator and device

Replies

struct ContentView: View {
    let a = 12.3456
    var body: some View {
        Text("\(a, specifier: "%.2f")")
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

This works on preview, simulator and device

Tell us how you really feel 😉


As it stands, neither Double.round, NumberFormatter, nor NSFormat have anything to do with SwiftUI. The first is part of the Swift standard library, and the latter two are part of the Foundation framework that's been around for about 30 years now.


As well as FedyaninT's suggestion of the specifier: argument within LocalizedStringKey's string interpolation implementation, NumberFormatter would also work when configured appropriately. Note that Double.round() is a mathematical function; it's used to round floating-point values to the closest integer value, and thus doesn't have anything to do with what you need.


NumberFormatter has a vast array of options, and will let you format integer and real values in almost every way defined as part of the Unicode and CLDR standard (the only exception I'm aware of is 'shortened' formats, e.g. "10 M" for "10000000"). To round a value to two decimal places you need to set a numberStyle of .decimal and set both .minimumFractionDigits and .maximumFractionDigits to '2'.


From memory, this is what you'd need:


static let formatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.minimumFractionDigits = 2
    formatter.maximumFractionDigits = 2
    return formatter
}()

...

let value = 123.456
Text("Your value is: \(value, formatter: formatter)") // "Your value is: 123.45"


Alternatively, if you're trimming to two decimal places in order to print a value in some currency, I would advise using NumberFormatter's .currency style instead:


static let formatter: NumberFormatter = {
  let formatter = NumberFormatter()
  formatter.numberStyle = .currency
  return formatter
}()

...

let value = 123.456
Text("Your value is: \(value, formatter: formatter)")
// "Your value is: $123.45" (or 123.45 €, if you're using the FR_fr locale)

ya, I was able to finally find that %.2f on stack overflow.

Took me several hours, and I was extremely frustrated with it at that point.

Thanks for the help.

%.2f is very usual formatting. No specific to SwiftUI.


So, don't hate, just learn 🙂

Wish you good.