最近有个朋友问我怎么根据一个数计算出这个数为几个小时或分钟的算法,比如500秒,转换为×小时×分×秒,我抽空写了一个程序,贴在这里吧。
首先定义几个公共变量
public
long
hour, munute, seconds;
然后是获得小时分钟和秒的方法
protected
void
gethour(
double
num)
... {
if (num >= 60 * 60)
...{
hour = (long)(num / 3600);
getminute(num%3600);
}
else
...{
hour = 0;
getminute(num);
}
}
protected void getminute( double num)
... {
if (num >= 60)
...{
munute = (long)(num / 60);
getsecond(num%60);
}
else
...{
munute = 0;
getsecond(num);
}
}
protected void getsecond( double num)
... {
seconds = (long)num;
}
... {
if (num >= 60 * 60)
...{
hour = (long)(num / 3600);
getminute(num%3600);
}
else
...{
hour = 0;
getminute(num);
}
}
protected void getminute( double num)
... {
if (num >= 60)
...{
munute = (long)(num / 60);
getsecond(num%60);
}
else
...{
munute = 0;
getsecond(num);
}
}
protected void getsecond( double num)
... {
seconds = (long)num;
}
执行获得小时的方法,传入一个值,就会获得改数字对应的小时分钟以及秒,代码不多,方法也很简单,不做过多解释了。
还有另外一个方法,也许更简单
public
void
text(
double
num)
... {
munute = (long)num / 60;
seconds = (long)num % 60;
if (munute > 60)
...{ hour = (long)munute / 60; munute = munute % 60; }
}
... {
munute = (long)num / 60;
seconds = (long)num % 60;
if (munute > 60)
...{ hour = (long)munute / 60; munute = munute % 60; }
}