问题
Not terribly familiar with Xcode or Objective-C but trying to learn. Hopefully someone can help me out with a problem I'm having.
I have two fields, one called price
and one called units
and I'm trying divide the inputs of the cells by each other and then display the result with the correct currency of the 'nationality' of the device when a button is pressed.
So far I have of the action of the button I have;
- (IBAction)calculate:(id)sender {
int x = [price.text floatValue];
int y = [units.text floatValue];
int calc_result = x / y;
self.result.text = [NSString stringWithFormat:@"%.2f", calc_result];
}
which outputs a result into a label field WITHOUT the decimal remainder.
How can I get it to display the decimal remainder to 2 decimal places and put in front the currency found from the 'nationality' of the device.
Thanks in advance!
回答1:
You are using an integer here:
int x = [price.text floatValue];
int y = [units.text floatValue];
int calc_result = x / y;
You should use a floating point number:
float x = [price.text floatValue];
float y = [units.text floatValue];
float calc_result = x / y;
来源:https://stackoverflow.com/questions/17515673/division-in-ios-xcode