Hello
I'm using Measurement(...) to convert watts to femtowatts, but if I convert 1 watts to femtowatts I get 999999999999999.9 instead of 1e+15, I have no idea on how to fix it, any help or ideas, here is the code
:
Thank you for your time
(Is there a way to use NumberFormatter with the computed String)
I'm using Measurement(...) to convert watts to femtowatts, but if I convert 1 watts to femtowatts I get 999999999999999.9 instead of 1e+15, I have no idea on how to fix it, any help or ideas, here is the code
:
Code Block import SwiftUI struct PowerView: View { @State private var inputValue = "" let inputUnits = [ "watts", "femtowatts" ] let outputUnits = [ "watts", "femtowatts" ] @State private var inputUnitValue = 0 @State private var outputUnitValue = 1 var after: String{ var input = Measurement(value: 0, unit: UnitPower.watts) var output: String = "" switch inputUnits[inputUnitValue] { case "watts": input = Measurement(value: Double(inputValue) ?? 0, unit: UnitPower.watts) case "femtowatts": input = Measurement(value: Double(inputValue) ?? 0, unit: UnitPower.femtowatts) default: input = Measurement(value: Double(inputValue) ?? 0, unit: UnitPower.watts) } switch outputUnits[outputUnitValue] { case "watts": output = String(describing: input.converted(to: UnitPower.watts)) case "femtowatts": output = String(describing: input.converted(to: UnitPower.femtowatts)) default: output = String(describing: input.converted(to: UnitPower.watts)) } return output } var body: some View { NavigationView{ Form{ Section(header: Text("Enter your Input value")){ TextField("Have a goal?", text: $inputValue) } Section(header: Text("Input")){ Picker("Input values", selection: $inputUnitValue){ ForEach(0..<inputUnits.count){ item in Text(inputUnits[item]) } } } Section(header: Text("Output")){ Picker("Output values", selection: $outputUnitValue){ ForEach(0..<outputUnits.count){ item in Text(outputUnits[item]) } } } Section(header: Text("Check your Output value")){ Text("\(after)") } } .navigationBarTitle("Pressure") } } }
Thank you for your time
(Is there a way to use NumberFormatter with the computed String)
When you create a String for showing Measurement to users, you should better use MeasurementFormatter when converting to String.(Is there a way to use NumberFormatter with the computed String)
Something like this:
Code Block struct PowerView: View { let outputFormatter: MeasurementFormatter = { let nf = NumberFormatter() //Set number style as you like... nf.numberStyle = .none nf.maximumSignificantDigits = 15 nf.usesSignificantDigits = true let mf = MeasurementFormatter() mf.numberFormatter = nf mf.unitOptions = .providedUnit return mf }() //... var after: String { let input: Measurement<UnitPower> let output: String switch inputUnits[inputUnitValue] { case "watts": input = Measurement(value: Double(inputValue) ?? 0, unit: .watts) case "femtowatts": input = Measurement(value: Double(inputValue) ?? 0, unit: .femtowatts) default: input = Measurement(value: Double(inputValue) ?? 0, unit: .watts) } switch outputUnits[outputUnitValue] { case "watts": output = outputFormatter.string(from: input.converted(to: .watts)) case "femtowatts": output = outputFormatter.string(from: input.converted(to: .femtowatts)) default: output = outputFormatter.string(from: input.converted(to: .watts)) } return output } //... }