Division in iOS Xcode

[亡魂溺海] 提交于 2019-12-25 01:49:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!