......的含义[停止

时间:2012-09-01 01:46:17

标签: ios xcode

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

这是一个例子。

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分○○秒

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

3 个答案:

答案 0 :(得分:10)

它被称为modulo operation。当你划分一个数字(并且只考虑整数)时,它会留下什么。

示例:

3 % 2 = 1
6 % 2 = 0
6 % 3 = 0
6 % 4 = 2

答案 1 :(得分:3)

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

因此,您可以在开始时添加天数的分隔,然后使用modulo获取在几天内删除后的分数:

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

答案 2 :(得分: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秒。