iOS计时器/工资计算器

时间:2014-04-24 03:02:51

标签: ios objective-c

我正在制作一个计时器,它需要每小时工资投入,并跟踪每秒赚多少钱。计时器上的格式化工作正常,但一旦便士点击99它就会零,并且只会增加美元。这就是我所拥有的。

seconds = (int) (elapsedTime = (elapsedTime - (minutes *60)));
pennies = seconds * (ratePerSecond * 100);
if(pennies > 99){

    dollars++;
    pennies = 0;
}

self.moneyDisplay.text = [NSString stringWithFormat: @"$%02d.%02d", dollars, pennies];

2 个答案:

答案 0 :(得分:2)

试试这个:

seconds = (int) (elapsedTime = (elapsedTime - (minutes *60)));
pennies = seconds * (ratePerSecond * 100);
float dollars = pennies / 100.0;
self.moneyDisplay.text = [NSString stringWithFormat: @"$%.2f", dollars];

或:

seconds = (int) (elapsedTime = (elapsedTime - (minutes *60)));
pennies = seconds * (ratePerSecond * 100);
int cents = pennies % 100;
int dollars = pennies / 100;
self.moneyDisplay.text = [NSString stringWithFormat: @"$%02d.%02d", dollars, cents];

答案 1 :(得分:0)

可能你想这样做(没有条件):

dollars = pennies / 100;
pennies = pennies % 100;
self.moneyDisplay.text = [NSString stringWithFormat: @"$%02d.%02d", dollars, pennies];

请注意:%运算符的操作数应具有整数类型。

希望这会有所帮助.. :)