1

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)
Craig Shine
  • 341
  • 1
  • 4
  • 12
  • What's the error? Which line? –  Aug 05 '19 at 02:27
  • If I add this Text("\(NSString(format: "%.2f", litreValue))") to the line : // Display as Single Digit example 2, or 3, or 7 etc Text("\(litreValue)") I get an error. 'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert? – Craig Shine Aug 05 '19 at 02:36

2 Answers2

5

If you wish to display an integer rather than a double, you can cast it into an integer and then display it.

Text("\(Int(litreValue))")
sfung3
  • 2,227
  • 1
  • 9
  • 30
  • 1
    By golly that worked... thank you.. As I am very new to this I may have more questions.. Thank you to the fantastic people on this forum. – Craig Shine Aug 05 '19 at 02:42
2

You can use Text(String(format: "%.2f", litreValue)). That won't give an integer display but it is what you used before. I think that the Int() is probably more efficient but this is the way to use formats in Swift.

Michael Salmon
  • 1,056
  • 7
  • 15