我试图显示 Double 中的单个值
Im trying to display a single value from a Double
我正在尝试显示步进器的单个值。
显示 Stepper 在 Text interpellation 中显示 Double 值。
例如,我将值 litreValue 设置为 Double,然后使用 Stepper 我可以 select 一个值,例如 8,但是我希望它在文本字段中显示为 8 而不是8.000000.
我已经尝试了 Text("(NSString(format: "%.2f", litreValue))") 但出现错误。
有什么建议吗
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)
如果您希望显示整数而不是双精度数,可以将其转换为整数然后显示。
Text("\(Int(litreValue))")
您可以使用 Text(String(format: "%.2f", litreValue))
。这不会给出整数显示,但它是您之前使用的。我认为 Int() 可能更有效,但这是在 Swift.
中使用格式的方式
我正在尝试显示步进器的单个值。
显示 Stepper 在 Text interpellation 中显示 Double 值。
例如,我将值 litreValue 设置为 Double,然后使用 Stepper 我可以 select 一个值,例如 8,但是我希望它在文本字段中显示为 8 而不是8.000000.
我已经尝试了 Text("(NSString(format: "%.2f", litreValue))") 但出现错误。
有什么建议吗
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)
如果您希望显示整数而不是双精度数,可以将其转换为整数然后显示。
Text("\(Int(litreValue))")
您可以使用 Text(String(format: "%.2f", litreValue))
。这不会给出整数显示,但它是您之前使用的。我认为 Int() 可能更有效,但这是在 Swift.