将小数和美国货币符号添加到单视图应用程序

约书亚·哈特(Joshua Hart)

首先,我要感谢网站上过去曾回答过我许多问题的开发人员和程序员。直到今天,我才注册了stackoverflow。你们中许多人提供给其他人的答案对我有很大帮助!谢谢!

我有一个正在使用的应用程序,该应用程序将每日薪水乘以工作天数,再乘以联邦税收百分比,然后为用户提供净收入和总收入金额。

我遇到的问题是我无法弄清楚如何在我的净收入和总收入文本字段中实现“ $”。

其次,我想表示我对此完全不满意,因此我很抱歉这个问题可能使您说:“哈,真的是乔什吗?这是9岁儿童的编码!” 我从较早的文章中复制了一些代码,并将其植入我的代码中,因此看起来似乎不对。如果您可以看一下代码并告诉我需要做什么,我将不胜感激!谢谢!

@implementation CRPDViewController
@synthesize dailyRateTextField;
@synthesize daysWorkedTextField;
@synthesize grossIncomeTextField;

- (IBAction)calculateButtonPressed:(UIButton *)sender
{
    int result = [dailyRateTextField.text intValue] * [daysWorkedTextField.text intValue];
    grossIncomeTextField.text = [NSString stringWithFormat:@"%.d", result];
    float taxRate = .72;
    float grossPay = [self.grossIncomeTextField.text floatValue];
    float netPay = grossPay * taxRate;
    self.netIncomeTextField.text = [NSString stringWithFormat:@"%.f", netPay];

    // alloc formatter
    NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];

    // set options.
    [currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];

    NSNumber *amount = [NSNumber numberWithInteger:78000];

    // get formatted string
    NSString* netPay = [currencyStyle stringFromNumber:amount]

    [currencyStyle release];

}

请尽可能以外行的方式给出解释!再次感谢!:)

约书亚·哈特(Joshua Hart)

尼丁·戈赫尔

要只设置$符号,请使用:

[NSString stringWithFormat:@"$%.f", netPay];

并将浮点数设置为2个小数位,请尝试如下操作:

 self.netIncomeTextField.text = [NSString stringWithFormat:@"$%.2f", netPay];

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章