我正在制作一个计时器,需要一个小时的工资输入并跟踪每秒赚多少钱。计时器上的格式工作得很好,但是一旦硬币达到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];

最佳答案

尝试这个:

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];

10-05 20:05