Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/Crash fixes #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions Calculator/Controller/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by Angela Yu on 10/09/2019.
// Copyright © 2019 London App Brewery. All rights reserved.
// Updated by OsamaSaberB
//

import UIKit
Expand All @@ -22,7 +23,15 @@ class ViewController: UIViewController {
return number
}
set {
displayLabel.text = String(newValue)

// To prevent AC button from showing displayLabel.text as "0.0" and display int "0" instead
if String(newValue) != "0.0" {

displayLabel.text = String(format: newValue.removeZerosFromEnd())

} else {
displayLabel.text = "0"
}
}
}

Expand Down Expand Up @@ -52,22 +61,45 @@ class ViewController: UIViewController {
if let numValue = sender.currentTitle {

if isFinishedTypingNumber {

//To prevent display label from showing only "." as first character and display "0." instead
if numValue == "." && displayLabel.text?.first != "." {
displayLabel.text = "0."
isFinishedTypingNumber = false
return
}
displayLabel.text = numValue
isFinishedTypingNumber = false

} else {

if numValue == "." {

let isInt = floor(displayValue) == displayValue

if !isInt {
// to prevent the addision of another (.)
if displayLabel.text!.contains(".") {
return
} else {
let isInt = floor(displayValue) == displayValue // Bool expresion
if !isInt {
return
}
}
}

} // end if numValue
displayLabel.text = displayLabel.text! + numValue
}
}
}

}

// extension to remove trailing zeros from an integer result
extension Double {
func removeZerosFromEnd() -> String {
let formatter = NumberFormatter()
let number = NSNumber(value: self)
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 16 //maximum digits in Double after dot (maximum precision)
return String(formatter.string(from: number) ?? "")
}
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Calculator-Advanced-Swift-iOS13-Completed-Updated-
The same calculator app from AppBrewery but with bugs fixed

- display label now shows Int characters properly instead of doubles with trailing zeros (i.e 1 instead of 1.0)
- adjusted the AC button to do the same and displaying “0” int instead of “0.0” when clearing screen
- adjusted the decimal/period button to start with 0. instead of displaying “.” as the first character when no number was entered
- prevented the addition of multiple decimals in a number which leads to crashing the app