【问题标题】:how would i set this app up to accept localized currency and output to that currency我将如何设置此应用程序以接受本地化货币并输出到该货币
【发布时间】:2013-05-03 05:58:05
【问题描述】:

您好,我正在尝试为我的应用设置本地化,它目前从文本框中接收字符串,并将它们转换为双精度。然后它继续进行计算并将其作为字符串输出到屏幕上

我想做的是, 让应用程序知道他们来自特定国家/地区将字符串转换为当地货币, 进行计算,然后以相关货币输出,

我认为解决方案在于 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];

}

【问题讨论】:

    标签: iphone ios alpha mask togglebutton


    【解决方案1】:

    NSNumberFormatter 包含一种货币样式,它是语言环境安全的,但不是线程安全的,因此不应从不同的线程修改它们。下面是一个例子:

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:tCost/100.00]];
    

    【讨论】:

    • 谢谢,但我不明白如何使用它来提取我的字符串并将其设置为本地货币,然后将其作为货币输出
    • 计算 tCost 后,将这三行放入并将 self.textField.text 设置为 numberAsString
    • 我的伙伴我让它工作了,在一个存根程序上很好,测试一个领域,太棒了:) 非常感谢,我会进一步测试它:) 看看我是怎么做的
    • 我会,但有一个问题,我的输出似乎总是比它应该的多 100 倍,例如,当标准测试的答案应该是 15 便士时,我现在得到 15.00 英镑,所以0.15 我将如何相应地改变输出,
    • 我的错现在是你的。谢谢队友,我真的很感激,你给出了一个简洁的答案,我可以从中学习和应用自己,
    猜你喜欢
    • 1970-01-01
    • 2011-07-02
    • 2023-04-04
    • 2021-11-20
    • 2016-10-10
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2013-04-20
    相关资源
    最近更新 更多