Chr*_*low 0 math currency objective-c division ios
不是非常熟悉Xcode或Objective-C,而是试图学习.希望有人可以帮我解决我遇到的问题.
我有两个字段,一个调用price,一个调用units,我正在尝试将单元格的输入相互分开,然后在按下按钮时使用设备的"国籍"的正确货币显示结果.
到目前为止,我已经掌握了按钮的动作;
- (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];
}
Run Code Online (Sandbox Code Playgroud)
它将结果输出到标签字段而没有小数余数.
如何让它将小数余数显示为2位小数,并将从设备的"国籍"中找到的货币放在前面.
提前致谢!
你在这里使用一个整数:
int x = [price.text floatValue];
int y = [units.text floatValue];
int calc_result = x / y;
Run Code Online (Sandbox Code Playgroud)
您应该使用浮点数:
float x = [price.text floatValue];
float y = [units.text floatValue];
float calc_result = x / y;
Run Code Online (Sandbox Code Playgroud)