ios - iOS Xcode 中的除法

标签 ios objective-c math currency division

不太熟悉 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];

}

将结果输出到不带小数余数的标签字段。

我怎样才能让它显示小数点后 2 位小数并将从设备的“国籍”中找到的货币放在前面。

提前致谢!

最佳答案

你在这里使用了一个整数:

int x = [price.text floatValue];
int y = [units.text floatValue];
int calc_result = x / y;

你应该使用 float :

float x = [price.text floatValue];
float y = [units.text floatValue];
float calc_result = x / y;

关于ios - iOS Xcode 中的除法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17515673/

相关文章:

objective-c - objective-C中 block 捕获变量的规则

IOS 根据 NSMutableArray 值更改 Uitableview 中的 Accessory View Cell Image

c# - 将 Math.Pow 与 Microsoft Solver Foundation 结合使用

ios - 为什么 CGBitmapContextCreateImage 比 [UIImage initWithData :]? 慢

iphone - 将类添加到 iphone 项目时出错

ios - 如何从 TableView 中删除行?

java - 计算器仅在第一次运行

iphone - 相对于背景图像的负文本颜色

ios - Cordova 3.3 到 3.4,iOS 插件不加载

php - 固定比例选择