How to display an int without commas?

Viewed 1807

I have a list of Text views that include a year saved as an int. I'm displaying it in an interpolated string:

Text("\($0.property) \($0.year) \($0.etc)")

The problem, it adds a comma to the int, for example it displays 1,944 instead of the year 1944. I'm sure this is a simple fix but i've been unable to figure out how to remove the comma. Thanks!

3 Answers

There is explicit Text(verbatim:) constructor to render string without localization formatting, so a solution for your case is

Text(verbatim: "\($0.property) \($0.year) \($0.etc)")

Use Text(String(yourIntValue)) if you use interpolation you need to cast it as a string directly. If you allow the int to handle it, it shows with a ,.

So to recap.

let yourIntValue = 1234
Text(String(yourIntValue)) // will return `1234`.
Text("\(yourIntValue)") // will return `1,234`.

I use the built-in format parameter. It's useful for formatting well beyond just this one specific usage (no commas).

Text("Disk Cache \(URLCache.shared.currentDiskUsage,
 format: .number.grouping(.never))")) 
Related