Hello
Is there a way to append a number to a Double, like this:
Thank you
Is there a way to append a number to a Double, like this:
Code Block var num: Double = 1 var num2 = num.append(1) //num2 should be 11
Thank you
As already suggested, you should better hold the String representation of displayed digits and the Double value of it separately, but mutually dependent.
In your case, computed property will work:
You may need to improve many parts, but with this, you can append that number to res.
In your case, computed property will work:
Code Block struct ContentView: View { //... private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())] //@State var res: Double = 0 @State var digits: String = "0" var res: Double { get { digits.hasSuffix(".") ? Double(digits.dropLast()) ?? 0.0 : Double(digits) ?? 0.0 } nonmutating set { digits = String(format: "%.10g", newValue) //<- May need better formatting } } @State var previousVar: Double = 0 //... var body: some View { VStack{ Spacer() HStack{ Spacer() Text("\(digits)") //<- .font(.system(size: 50)) .bold() .padding() } //... switch numeri[i][j] { case Nums.AC: operat = Nums.AC; res = 0 case Nums.uguale: operat = Nums.uguale; res = final case Nums.somma: operat = Nums.somma; previousVar = res; res = 0 con = 0 case Nums.meno: operat = Nums.meno; previousVar = res; res = 0 con = 0 case Nums.divisione: operat = Nums.divisione; previousVar = res; res = 0 con = 0 case Nums.moltiplicazione: operat = Nums.moltiplicazione; previousVar = res; res = 0 con = 0 case Nums.percentuale: operat = Nums.percentuale; res = res / 100 case Nums.piùMeno: operat = Nums.piùMeno; //res = res * -1; if digits.hasPrefix("-") { digits = String(digits.dropFirst()) } else { digits = "-" + digits } con = 0 case Nums.virgola: operat = Nums.virgola; //res = Double(String(res) + ".") ?? 0 if !digits.contains(".") { digits += "." } default: //res = Double(numeri[i][j].rawValue) ?? 0 if digits == "0" { digits = numeri[i][j].rawValue } else { digits += numeri[i][j].rawValue } con += 1 } //... }.padding(2) } }
You may need to improve many parts, but with this, you can append that number to res.