UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Displaying a double with 2 decimal places in an expandable list

Forums > Swift

Hi! I have the following expandable list. kpiValue is a double but I only want to display it with 2 decimal places (in fact it a revenue number) How do I do this in an expandable list specifier: "%.2f does not work. I get the error: Trailing closure passed to parameter of type 'FormStyleConfiguration' that does not accept a closure at the top of my form.

 // "Month to Date" Expandable list
                                   let itemsMthAv: [BookmarkMthAv] = [.kpiListMthAv]
                                   List(itemsMthAv, children: \.items) { row in
                                               HStack {
                                                   Image(systemName: row.icon)
                                                   Text(row.name)
                                                       .font(.subheadline)
                                                       .foregroundColor(.purple)
                                                   Text(row.currency)
                                                       .font(.subheadline)
                                                       .foregroundColor(.green)
                                                  // Text(row.kpiValue, specifier: "%.2f" )
                                                        Text(row.kpiValue)
                                                       .font(.subheadline)
                                                       .foregroundColor(.green)
                                               }
                                           }

2      

Try

Text("\(row.kpiValue, specifier: "%.2f")")

2      

Hello, Yes I tried that but sadly no progress.

Whan I make that change I get a compile error:

FormStyleConfiguration' that does not accept a closure at the top of my formI dont understand why, I can only assume that using specifier breaks something else in the form.

2      

ah. I re read your response. My bad! Your syntax is different!. I will try this. Thank you.

2      

Sadly no success: Instance method 'appendInterpolation(_:specifier:)' requires that 'String' conform to '_FormatSpecifiable'

2      

Tried this and works fine

struct ContentView: View {
    let kpiValue = 2.34567

    var body: some View {
        VStack {
            Text("\(kpiValue, specifier: "%.2f")")
        }
        .padding()
    }
}

Are you sure that kpiValue is a Double not a String because this get the error

struct ContentView: View {
    let kpiValue = "2.34567"

    var body: some View {
        VStack {
            Text("\(kpiValue, specifier: "%.2f")")
        }
        .padding()
    }
}

if so you might want to do something like this

struct ContentView: View {
    let kpiValue = "2.34567"

    var kpiValueNumber: Double {
        guard let kpiValue = Double(kpiValue) else { return 0.0 }
        return kpiValue
    }

    var body: some View {
        VStack {
            Text("\(kpiValueNumber, specifier: "%.2f")")
        }
        .padding()
    }
}

2      

That was it Nigel. Pilot error on my side.

        Text("\(kpiValue, specifier: "%.2f")")

and thank you @Hatsushira!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.