I am trying to display a single value from a Stepper.
Displaying the Stepper show a Double value within the Text interpellation.
For example I set the value litreValue as a Double, then using the Stepper I can select a value, for example 8, however I wish it to display in the text field as just 8 and not as 8.000000.
I have tried Text("(NSString(format: "%.2f", litreValue))") bur get an error.
Any suggestions please.
import SwiftUI
struct ContentView: View {
@State private var litreValue : Double = 0.00
@State private var selection = 0
let mixRatio = ["10:1","15:1","20:1","25:1","30:1","35:1","40:1","45:1","50:1","55:1","60:1","65:1","70:1"]
var body: some View {
VStack{
ZStack{
RoundedRectangle(cornerRadius: /*@START_MENU_TOKEN@*/20/*@END_MENU_TOKEN@*/)
.foregroundColor(/*@START_MENU_TOKEN@*/.blue/*@END_MENU_TOKEN@*/)
.frame(width: 300.0, height: 200.0)
.shadow(radius: /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
VStack {
Image("Fuel-Icon2")
.resizable()
.frame(width: /*@START_MENU_TOKEN@*/100.0/*@END_MENU_TOKEN@*/, height: /*@START_MENU_TOKEN@*/100.0/*@END_MENU_TOKEN@*/)
Text("Fuel Mix")
.font(.largeTitle)
}
}
VStack {
Stepper(value: $litreValue, in: 1...200) {
Text("Litres")
.font(.title)
}.padding(.top, -30)
ZStack{
RoundedRectangle(cornerRadius: /*@START_MENU_TOKEN@*/20/*@END_MENU_TOKEN@*/)
.foregroundColor(.gray)
.frame(width: 300.0, height: 50.0)
// Display as Single Digit example 2, or 3, or 7 etc
Text("\(litreValue)")
.font(.largeTitle)
.fontWeight(.semibold)
}
}.padding(30)
VStack{
Picker(selection: $selection, label:
Text("")
.padding(.top))
{
ForEach(0 ..< mixRatio.count) { index in
Text(self.mixRatio[index]).tag(index)
}
}
}
.padding(.top, -60)
.padding(.horizontal, 50)