Part 1: Formatting a number with thousands separator and 10 digit limitation in Xcode CS193P
https://forums.macrumors.com/threads/1625618/
I've been trying to figure out display numbers with thousand separator on CS193P based iOS calculator app.
I've succeed convert numbers like 1000 to 1,000 for view. But I cannot find a way to let model read number with 1,000 in display as 1000 and return result as number with same format like 1,000 + 1,000 = 2,000.
Now my calculator app works like 1,000 + 1,000 = 1,000 or 1,000 + 3,000 = 3,000. It's seems = doesn't work.
When I pressed second number after pressed operation button such as +, -, *, /, second number shows correctly.
1. enter 1000, display shows 1,000
2. enter +, display holds 1,000
3. enter second number 3000, display shows 3,000
4. press =, display still holds 3,000
I put my code in below. Please take a look how it's works.
From test result and code in above, I guess NSNumberFormatter works for view(display) but doesn't work for model(brain).
And can I say this in another word like, NSnumberFormatter works for NSStirng, but doesn't work for double value as it's self?
If anyone find way or idea about how operation button get's works correctly, please let me know it. That would be really appreciate.
Thanks in advance,
https://forums.macrumors.com/threads/1625618/
I've been trying to figure out display numbers with thousand separator on CS193P based iOS calculator app.
I've succeed convert numbers like 1000 to 1,000 for view. But I cannot find a way to let model read number with 1,000 in display as 1000 and return result as number with same format like 1,000 + 1,000 = 2,000.
Now my calculator app works like 1,000 + 1,000 = 1,000 or 1,000 + 3,000 = 3,000. It's seems = doesn't work.
When I pressed second number after pressed operation button such as +, -, *, /, second number shows correctly.
1. enter 1000, display shows 1,000
2. enter +, display holds 1,000
3. enter second number 3000, display shows 3,000
4. press =, display still holds 3,000
I put my code in below. Please take a look how it's works.
Code:
//
// in CalculatorViewController.m
//
- (IBAction)operationPressed:(UIButton *)sender
{
if(userIsInTheMiddleOfTypingANumber){
self.brain.operand = [display.text doubleValue];
userIsInTheMiddleOfTypingANumber = NO;
}
NSString *operation = [[sender titleLabel] text];
NSLog(@"operation pressed = %@", operation);
[self.brain performOperation:operation];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *c = [numberFormatter numberFromString:display.text];
[display setText:[numberFormatter stringFromNumber:c]];
}
Code:
//
// in CalculatorBrain.m
//
- (void)performWaitingOperation {
if ([@"+" isEqual:waitingOperation]) {
operand = waitingOperand + operand;
} else if ([@"-" isEqual:waitingOperation]) {
operand = waitingOperand - operand;
} else if ([@"*" isEqual:waitingOperation]) {
operand = waitingOperand * operand;
} else if ([@"/" isEqual:waitingOperation]) {
if (operand) {
operand = waitingOperand / operand;
}
}
}
- (double)performOperation:(NSString *)operation
{
if ([@"C" isEqual:operation]){
operand = 0;
waitingOperand = 0;
waitingOperation = 0;
storedValue = 0;
isPending = NO;
} else {
[self performWaitingOperation];
waitingOperation = operation;
waitingOperand = operand;
isPending = YES;
}
return operand;
}
From test result and code in above, I guess NSNumberFormatter works for view(display) but doesn't work for model(brain).
And can I say this in another word like, NSnumberFormatter works for NSStirng, but doesn't work for double value as it's self?
If anyone find way or idea about how operation button get's works correctly, please let me know it. That would be really appreciate.
Thanks in advance,
Last edited: