diff --git a/Calculator/Calculator.xcodeproj/project.pbxproj b/Calculator/Calculator.xcodeproj/project.pbxproj index 70b7a3f..1368575 100644 --- a/Calculator/Calculator.xcodeproj/project.pbxproj +++ b/Calculator/Calculator.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ C6586B6D1E7EB8B700AE8920 /* CalculatorUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6586B6C1E7EB8B700AE8920 /* CalculatorUITests.swift */; }; + C6A38E051E91592900064B44 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6A38E041E91592900064B44 /* Constants.swift */; }; C6CE27911E7D733D004FCFC3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6CE27901E7D733D004FCFC3 /* AppDelegate.swift */; }; C6CE27931E7D733D004FCFC3 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6CE27921E7D733D004FCFC3 /* ViewController.swift */; }; C6CE27961E7D733D004FCFC3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C6CE27941E7D733D004FCFC3 /* Main.storyboard */; }; @@ -39,6 +40,7 @@ C6586B6A1E7EB8B700AE8920 /* CalculatorUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CalculatorUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; C6586B6C1E7EB8B700AE8920 /* CalculatorUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalculatorUITests.swift; sourceTree = ""; }; C6586B6E1E7EB8B700AE8920 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + C6A38E041E91592900064B44 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; C6CE278D1E7D733D004FCFC3 /* Calculator.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Calculator.app; sourceTree = BUILT_PRODUCTS_DIR; }; C6CE27901E7D733D004FCFC3 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; C6CE27921E7D733D004FCFC3 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -113,6 +115,7 @@ C6CE27921E7D733D004FCFC3 /* ViewController.swift */, C6CE27941E7D733D004FCFC3 /* Main.storyboard */, C6F47CD41E7D969700E92C9E /* CalculatorBrain.swift */, + C6A38E041E91592900064B44 /* Constants.swift */, C6CE27B01E7D7391004FCFC3 /* Supporting Files */, ); path = Calculator; @@ -287,6 +290,7 @@ C6F47CD51E7D969700E92C9E /* CalculatorBrain.swift in Sources */, C6CE27931E7D733D004FCFC3 /* ViewController.swift in Sources */, C6CE27911E7D733D004FCFC3 /* AppDelegate.swift in Sources */, + C6A38E051E91592900064B44 /* Constants.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Calculator/Calculator/CalculatorBrain.swift b/Calculator/Calculator/CalculatorBrain.swift index 9ed5d22..022ebbf 100644 --- a/Calculator/Calculator/CalculatorBrain.swift +++ b/Calculator/Calculator/CalculatorBrain.swift @@ -90,7 +90,12 @@ struct CalculatorBrain { mutating func setOperand(_ operand: Double) { accumulator = operand - accumulatorString = String(format: "%g", operand) + + let numberFormatter = NumberFormatter() + numberFormatter.numberStyle = .decimal + numberFormatter.usesGroupingSeparator = false + numberFormatter.maximumFractionDigits = Constants.numberOfDigitsAfterDecimalPoint + accumulatorString = numberFormatter.string(from: NSNumber(value: operand)) } var result: Double? { diff --git a/Calculator/Calculator/Constants.swift b/Calculator/Calculator/Constants.swift new file mode 100644 index 0000000..844d315 --- /dev/null +++ b/Calculator/Calculator/Constants.swift @@ -0,0 +1,13 @@ +// +// Constants.swift +// Calculator +// +// Created by Peter Vanhoef on 2/04/17. +// Copyright © 2017 Peter Vanhoef. All rights reserved. +// + +import Foundation + +struct Constants { + static let numberOfDigitsAfterDecimalPoint = 6 +} diff --git a/Calculator/Calculator/ViewController.swift b/Calculator/Calculator/ViewController.swift index 3e4a7cd..f0d0243 100644 --- a/Calculator/Calculator/ViewController.swift +++ b/Calculator/Calculator/ViewController.swift @@ -33,7 +33,11 @@ class ViewController: UIViewController { return Double(display.text!)! } set { - display.text = String(format: "%g", newValue) + let numberFormatter = NumberFormatter() + numberFormatter.numberStyle = .decimal + numberFormatter.usesGroupingSeparator = false + numberFormatter.maximumFractionDigits = Constants.numberOfDigitsAfterDecimalPoint + display.text = numberFormatter.string(from: NSNumber(value: newValue)) } } diff --git a/Calculator/CalculatorTests/CalculatorBrainTests.swift b/Calculator/CalculatorTests/CalculatorBrainTests.swift index 7d5a2d1..daca994 100644 --- a/Calculator/CalculatorTests/CalculatorBrainTests.swift +++ b/Calculator/CalculatorTests/CalculatorBrainTests.swift @@ -246,7 +246,7 @@ class CalculatorBrainTests: XCTestCase { testBrain.performOperation("+") testBrain.setOperand(1) testBrain.performOperation("=") - XCTAssertEqual(testBrain.description, "4.123456 + 1") + XCTAssertEqual(testBrain.description, "4.123457 + 1") XCTAssertFalse(testBrain.resultIsPending) XCTAssertTrue(abs(testBrain.result! - 5.123456) < 0.0001) } diff --git a/Calculator/CalculatorUITests/CalculatorUITests.swift b/Calculator/CalculatorUITests/CalculatorUITests.swift index 45eca10..2d3a80f 100644 --- a/Calculator/CalculatorUITests/CalculatorUITests.swift +++ b/Calculator/CalculatorUITests/CalculatorUITests.swift @@ -433,10 +433,10 @@ class CalculatorUITests: XCTestCase { app.buttons["8"].tap() app.buttons["9"].tap() app.buttons["+"].tap() - XCTAssert(app.staticTexts["4.123456 + …"].exists) + XCTAssert(app.staticTexts["4.123457 + …"].exists) app.buttons["1"].tap() app.buttons["="].tap() - XCTAssert(app.staticTexts["4.123456 + 1 ="].exists) - XCTAssert(app.staticTexts["5.123456"].exists) + XCTAssert(app.staticTexts["4.123457 + 1 ="].exists) + XCTAssert(app.staticTexts["5.123457"].exists) } }