I made a calculator in swift ui but the function I made for division works, but it doesn't work correctly, for example to make 25/2= 12.5 mine makes 25/2=12.0 so I round the result I put the function here Below if anyone can help me and write me the correct function, thank you very much in advance everyone :)
func buttonTapped(_ title: String) {
if title == "%" {
// Calcola la percentuale
if let number = Double(displayText) {
let result = number / 100.0
displayText = String(result)
let fullOperation = "\(number)% = \(result)"
addToHistory(fullOperation)
} else if title == "/" {
// Calcola la divisione
let components = displayText.components(separatedBy: "/")
if components.count == 2,
let numerator = Double(components[0]),
let denominator = Double(components[1]), denominator != 0 {
let result = numerator / denominator
displayText = String(result)
let fullOperation = "\(numerator) / \(denominator) = \(result)"
addToHistory(fullOperation)
} else {
// Gestisci la divisione per zero o formato non valido
displayText = "Errore"
addToHistory("Errore: Divisione non valida")
}
} //fine divisione
Post
Replies
Boosts
Views
Activity
I made a calculator in swift ui but the function I made for division works, but it doesn't work correctly, for example to make 25/2= 12.5 mine makes 25/2=12.0 so I round the result I put the function here Below if anyone can help me and write me the correct function, thank you very much in advance everyone :)
func calculate() {
let expression = NSExpression(format: displayText)
if let result = expression.expressionValue(with: nil, context: nil) as? Decimal {
let fullOperation = "\(displayText) = \(result)"
displayText = "\(result)"
history.append(fullOperation)
} else {
displayText = "Error"
}
}
import SwiftUI
import Firebase
struct ContentView: View {
@State var email: String = ""
@State var password: String = ""
@State var showLogin: Bool = true
var body: some View {
NavigationView{
VStack {
Image(systemName: "globe").resizable().frame(width: 150, height: 150).padding(20)
Text("Login").font(.system(size: 22)).foregroundColor(Color.black).padding(10)
Email(Email: self.$email).padding(10)
Password(Password: self.$password).padding(10)
NavigationLink(destination: RswPassword (email: self.$email, password: self.$password, showLogin: self.$showLogin)) {
Text("Reset Password ")
}.padding(20)
Button(action:{
//Procedura Login firebase
Auth.auth().signIn(withEmail: self.email, password: self.password) { (result, errore) in
if errore != nil {
print("errore login")
} else {
print("login effettuato")
}
}
}) {
Text("Accedi")
.font(.system(size: 14))
.foregroundColor(Color.black)
.padding()
.frame(minWidth: 0, maxHeight: .infinity)
.frame(height: 40)
}.padding(10)
Button(action :{
//procedura Registrazione Firebase
}) {
Text("Registrati ")
.font(.system(size: 13))
}
}
}
struct RswPAssword: View {
@Binding var email: String
@Binding var password: String
@Binding var showLogin: Bool
var body: some View {
VStack {
Image("globe").resizable().frame(width: 150, height: 150).padding(20)
Text("Resetta la password").padding(20).font(.system(size: 22)).foregroundColor(Color.black)
Email(Email: self.$email).padding(10)
Button (action: {
//procedura reset password
Auth.auth().sendPasswordReset(withEmail: self.email) { (error) in
if error != nil {
print("errore nel reset ")
} else {
print("email inviata con sucesso")
self.showLogin.toggle()
}
}
}) {
Text("resetta password")
.font(.system(size: 14))
.foregroundColor(Color.black)
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.frame(height: 40)
.background(Color.black)
}.padding(10)
}
}
struct Email: View {
@Binding var email: String
var body: some View {
TextField("Email", text: $email)
.font(.system(size: 14))
.padding(12)
.background(RoundedRectangle(cornerRadius: 5).stroke(Color.black))
.foregroundColor(Color.black)
}
}
struct Password: View {
@Binding var password: String
var body: some View {
TextField("Password", text: $password)
.font(.system(size: 14))
.padding(12)
.background(RoundedRectangle(cornerRadius: 5).stroke(Color.black))
.foregroundColor(Color.black)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
}
}