-4

这很愚蠢,但我必须知道 ( % ) 符号的含义,因为我想添加天数。

这是一个例子。

int seconds = 78120;
int forHours = (seconds1 / 3600),
    remainder = (seconds1 % 3600),
    forMinutes = remainder / 60,
    forSeconds = remainder % 60;
    NSString *Time = [NSString stringWithFormat:@"%02i:%02i:%02i",forHours,forMinutes,forSeconds];
    Label.text = Time;

结果:21:42:00

我希望结果像( 0天,21:42:00)一样(DD,HH:mm:ss

4

3 回答 3

10

这称为模运算。当你除以一个数字时剩下的就是(并且只考虑整数)。

例子:

3 % 2 = 1
6 % 2 = 0
6 % 3 = 0
6 % 4 = 2
于 2012-09-01T01:48:20.343 回答
3

%(模)给出除法后的余数。

因此,您可以在开始时添加天数,然后使用模数来获取以天为单位的删除后的秒数:

int seconds = 78120;
int days = seconds / 86400;

// Equivalent to: seconds = seconds - days * 86400 /*# seconds in a day*/;
seconds = seconds % 86400; // seconds remaining less than a day

int forHours = (seconds1 / 3600),
    remainder = (seconds1 % 3600), // seconds remaining within an hour
    forMinutes = remainder / 60,
    forSeconds = remainder % 60; // seconds remaining less than a minute
于 2012-09-01T01:50:57.377 回答
2

模数 (%) 运算符返回整数除法的余数。

a = 13% 5;

在这里,a 将等于 3。

尝试:

int fordays = seconds1 / 86400,
    remainder = seconds1 % 86400,
    forHours = remainder / 3600,
    remainder = remainder % 3600,
    forMinutes = remainder / 60,
    forSeconds = remainder % 60; 

1 天 = 86400 秒。

于 2012-09-01T01:53:18.200 回答