How do I just display plain numbers using SwiftUI? Text keeps adding commas

Viewed 528

I have been having an issue and have spent days trying to find a solution, I'm trying to learn SwiftUI having not done Swift in a while and only getting back into it post neurosurgery to remove brain tumour (so maybe I'm just a bit slow). I'd spend a bit of time trying to solve this then give up and move on only to come back to it, but I'm reaching the point where I need it sorted as little else needs done in my app update / conversion to SwiftUI.

I am trying to display a year (eg 2021) output as just a number off some previous code I did, this is not handled as a date for a reason I won't get into here. I'm just passing years as numbers. However when I put these into Text() in the middle of a string they are always output formatted like a normal number (eg 2,021) which obviously is not how years are displayed.

Basically what I need is just a way to tell Text() to ignore the fact it's a "number" and just display it as is with no adaption as if it is just string value as part of the string it's being added to.

Example

struct ContentView: View {

    let theYear = 2021
    
    var body: some View {
        VStack{
            Text("The year is \(theYear)")
            }
      }
} 

Will output

The year is 2,021

I've tried to solve this but I'm clearly missing something that I'm sure is an obvious answer, anyone have any tips or advice? Thanks.

1 Answers

You can just wrap it in a String().

Text("The year is \(String(theYear))")
Related