It seems like the issue might be related to how the result is displayed after the division operation. If you want to display decimal places for the result, you can use the String(format:) method to format the result with a specific number of decimal places.
Here's how you can modify the division part of your code to ensure it displays the result with the correct decimal places:
// 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(format: "%.1f", result) // Use "%.1f" for one decimal place
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
In the modified code, String(format: "%.1f", result) is used to format the result with one decimal place. Adjust the format string ("%.1f") according to the number of decimal places you want to display.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: