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