Hello
Do you know why the comma in this calculator app isn't working:
Maybe at line 51 and 52 or 121
(If you can explain my mistakes all around the code I would be grateful)
Thank you
Do you know why the comma in this calculator app isn't working:
Code Block import SwiftUI import Combine enum Nums: String{ case uno = "1" case due = "2" case tre = "3" case quattro = "4" case cinque = "5" case sei = "6" case sette = "7" case otto = "8" case nove = "9" case zero = "0" case moltiplicazione = "X" case divisione = "/" case somma = "+" case meno = "-" case uguale = "=" case AC = "AC" case piùMeno = "±" case percentuale = "%" case virgola = "." case niente = "" } struct ContentView: View { var numeri: [[Nums]] = [ [Nums.AC, Nums.piùMeno, Nums.percentuale, Nums.divisione], [Nums.sette, Nums.otto, Nums.nove, Nums.moltiplicazione], [Nums.quattro, Nums.cinque, Nums.sei, Nums.meno], [Nums.uno, Nums.due, Nums.tre, Nums.somma], [Nums.zero, Nums.niente, Nums.virgola, Nums.uguale] ] private var gridItemLayout = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())] @State var previousVar: Double = 0 let timer = Timer.publish(every: 0.001, on: .main, in: .common).autoconnect() @State var con: Int = 0 @State var final: Double = 0 @State var operat = Nums(rawValue: "") @State var digits: String = "0" var res: Double { get { //digits.hasSuffix(".") ? Double(digits.dropLast()) ?? 0.0 : Double(digits) ?? 0.0 digits.hasSuffix(".") ? Double(digits) ?? 0.0 : Double(digits) ?? 0.0 } nonmutating set { digits = String(format: "%.10g", newValue) //<- May need better formatting } } var body: some View { VStack{ Spacer() HStack{ Spacer() Text("\(digits)") .font(.system(size: 50)) .bold() .padding() } Spacer() LazyVGrid(columns: gridItemLayout, content: { ForEach(0..<5){ i in ForEach(0..<4){ j in RoundedRectangle(cornerRadius: 35.0) .foregroundColor(.orange) .frame(width: 80, height: 80, alignment: .center) .overlay( Text("\(numeri[i][j].rawValue)") .font(.largeTitle) .foregroundColor(.black) ).onTapGesture { 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; if digits.hasPrefix("-") { digits = String(digits.dropFirst()) } else { digits = "-" + digits } con = 0 case Nums.virgola: operat = Nums.virgola; if !digits.contains(".") { digits += "." } default: if digits == "0" { digits = numeri[i][j].rawValue } else { digits += numeri[i][j].rawValue } con += 1 } } } } }).onReceive(timer) { _ in if con != 0 { if operat == Nums.divisione{ final = previousVar / res } else if operat == Nums.meno{ final = previousVar - res } else if operat == Nums.moltiplicazione{ final = previousVar * res } else if operat == Nums.somma{ final = previousVar + res } } } }.padding(2) } }
Maybe at line 51 and 52 or 121
(If you can explain my mistakes all around the code I would be grateful)
Thank you