GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: remove thousands separators in swiftUI 2

Forums > SwiftUI

@BSM  

hi, in the app I am building, I display some info about a product. one is the price, a double. And the other is an int, the product code.

I have this code:

struct ContentView: View {
    var body: some View {
        VStack {
            HStack {
                Text("Product Name")
                    .font(.title)
                Spacer()
                Text("100")
            }.padding(.leading).padding(.trailing).padding(.top)
            Spacer()
            productInfo(price: 1234567868, code: 23456789).padding(.leading).padding(.trailing).padding(.bottom)
        }
        .frame(width: 360, height: 100 )
        .background(Color(UIColor.secondarySystemBackground))
        .cornerRadius(25)

    }
}

struct productInfo: View {
    var price: Double
    var code: Int

    var body: some View {
        VStack {
            HStack {
                Text("Product Price")
                Spacer()
                Text("$\(price, specifier: "%.2f")")
            }

            HStack {
                Text("Product Code")
                Spacer()
                Text("\(code)")
            }
        }
//        .frame(width: 250)

    }
}

when I run that, this is what I get(copy pasted from simulator)

the product code: 23,456,789

and the price: $1,234,567,868.00

what can I do to remove those commas from the view?

thanks!

3      

hi,

you should take a look at this post from a few days ago. it's about string interpolation.

hope that helps,

DMG

4      

@BSM  

thx! it helped!

3      

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

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.