So as we mentioned in the last lesson, our calculator has a bit of a logical issue. 1
It lets us add as many decimal points as we want 2
and this will dramatically screw up our app because when we try to, say, multiply this by minus 1 3
or we try to turn it into a percentage, 4
we have a fatal error and the error is, of course, cannot convert display labeled text to a Double because 5
I'm not even sure what 2.3.6.23 is as a number. 6
It's definitely not a decimal number. 7
All right, so what can we do instead? 8
Well, down here where we've got our numButtonPressed, we should be able to check to see if we currently 9
have an integer in here. 10
So, say, if I had an 8 in here, then I should be able to use the .button, but if I already have a Double, 11
say, 8.5, then I shouldn't be able to press this button or add that dot to the display any 12
more. 13
So in order to do that, we're going to tap into the else statement inside the numButtonPressed. 14
Because when isFinishedTypingNumber is true, then the current value in here will be 0. 15
It's only when we've got an actual number in here and we switched isFinishedTypingNumber to false 16
where we actually want to check if the current number value that's being typed in is equal to a full 17
stop, namely a decimal point. 18
And in that case, then we can say that let isInt, 19
so we're going to create a new local constant equal to floor. 20
So we're going to round down whatever is currently inside the displayLabel. 21
So it's expecting a Double, 22
so I have to convert our displayLabel to a Double, then I'm going to say displayLabel.text. 23
And now, I've got whatever it is that's currently inside my displayLabel rounded down to the floor. 24
So, say, if I had 8.1 in here, then it would round it down to 8. 25
So, now I want to be able to-- 26
So, now I want to be up to compare this with the non-rounded down version of my displayLabel.text. 27
Now, just as we saw previously, I can either force unwrap the Double initialization to say that this will 28
always be convertible to a Double, or what I want to do is to write a "guard" statement that checks whether 29
if this is, in fact, convertible to a Double, and also to add a fatal error and a message if it is not. 30
So same as what we did before. 31
Pause the video and see if you can complete this challenge. 32
All right. 33
So this is not so hard. 34
We're just going to write a "guard let," and we're going to say currentDisplayValue = 35
displayLabel.text 36
cast into a double. Now, if this is not true, i.e., fails, we get a nil, 37
then we want to throw a fatal error where we say "Cannot convert display label text to a Double!" 38
And now instead of using this number, we can simply use the currentDisplayalue over there and over 39
here. 40
So, now our code looks a little bit simpler and all it does is it says round down the 41
currentDisplayValue. 42
So if it's 8.1, round it down to 8. And that's what the floor method does, 43
it simply rounds our number down. 44
Now, the other part is comparing whether if the rounded down version of our display value is equal to 45
the currentDisplayValue. 46
So is 8.0 equal to 8.1? 47
Well, that's false, right? 48
So then, is Integer becomes false. 49
Now, however, if I had 8 in here instead, then 8 rounded down is equal to 8. 50
So isInt equal to true. 51
And so this means that we can check to see if we don't have an integer, 52
namely, we've got a number that has a decimal place in here, 53
then isInt is false. 54
So if isInt is false, then we're simply going to return. And this keyword forces our function to return, 55
and it means that we don't continue appending the current numValue which, remember, is a full stop to 56
the display. 57
So let's go ahead and run this and see what happens. 58
So let's try 59
8.1. 60
And then I'm going to try add another decimal place to the end of this. And you can see that no matter 61
how hard I press, it's not letting me. 62
But if I clear the screen and it was a single integer number, then I can add a single decimal place. 63
So that's sorted out our little problem there.