您好我正在尝试为我的应用设置本地化,它目前从文本框中收到字符串,并将它们转换为double。然后它继续进行计算并将其作为字符串输出到屏幕上
我想做的是, 让应用知道他们来自特定国家/地区将字符串转换为本地货币, 进行计算,然后以相关货币输出,
我认为解决方案就是nsformatter。我一直在查看文档并且遗失了,这是我的代码,你们会建议我们欢迎任何帮助。
(IBAction)calculateCost:(UIButton *)sender {
NSString *rate = self.textField1.text;
NSString *wat = self.textField2.text;
NSString *hours = self.textField3.text;
NSString *Days = self.textField4.text;
double num1 = [rate doubleValue];
double num2 = [wat doubleValue];
double num3 = [hours doubleValue];
double num4 = [Days doubleValue];
double appKw = num2 / 1000;
double costKwph = appKw *num1;
double tCost = ((num4 * num3) * costKwph);
if (num2 == 0 || num1 == 0 || num3 == 0 || num4 == 0) {
self.textField5.text = 0;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"oops" message:@"you must fill in all fields" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
[alert show];
}
tCost = tCost / 100;
self.textField5.text = [NSString stringWithFormat:@"£%0.2f",tCost];
}
答案 0 :(得分:0)
NSNumberFormatter包含货币样式,它是区域安全的,但不是线程安全的,因此不应该从不同的线程修改它们。一个例子如下:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:tCost/100.00]];