iOS Xcode中的除法 [英] Division in iOS Xcode
问题描述
对Xcode或Objective-C并不十分熟悉,但尝试学习.希望有人可以帮助我解决我遇到的问题.
Not terribly familiar with Xcode or Objective-C but trying to learn. Hopefully someone can help me out with a problem I'm having.
我有两个字段,一个称为price
,一个称为units
,我正在尝试将单元格的输入彼此分开,然后以正确的设备国籍"货币显示结果当按下按钮时.
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.
提前谢谢!
推荐答案
您在此处使用整数:
int x = [price.text floatValue];
int y = [units.text floatValue];
int calc_result = x / y;
您应该使用浮点数:
float x = [price.text floatValue];
float y = [units.text floatValue];
float calc_result = x / y;
这篇关于iOS Xcode中的除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!