I have a ForEach inside a picker with numbers from 0 to 20 that shows 0, 1, 2...20 but instead I need to display 00, 01, 02...20. How can I do it?
Asked
Active
Viewed 672 times
0
-
Do you want your output value to be a `String` to have a `0` in-front, or an `Int` which you can pad zeroes in-front later on? I feel like an `Int` would be more useful. – George Aug 09 '20 at 23:45
-
I actually have a string that stores the current data picker – xmetal Aug 09 '20 at 23:46
-
I just feel like you would need to check against each `String` value separately, but it's up to you. – George Aug 09 '20 at 23:47
-
what's the best way to achieve that? – xmetal Aug 09 '20 at 23:48
-
My current picker shows the output like this: ForEach(0 ..< 21) { item in Text("\(item)")} – xmetal Aug 09 '20 at 23:50
-
[New Dev](https://stackoverflow.com/a/63332441/9607863) has shown it best – George Aug 09 '20 at 23:51
1 Answers
3
You use the normal string formatting String(format: "%02d", 5)
:
@State var num: Int = 0
var body: some View {
Picker("", selection: $num) {
ForEach(0..<21) { v in
Text(String(format: "%02d", v))
}
}
}

New Dev
- 48,427
- 12
- 87
- 129