我在Java中遇到了一个关于非常大的数字的问题。我目前正在尝试将由大量秒组成的双精度数转换为年、天、小时、分钟和剩余的秒数。我的代码如下:
import java.util.Scanner;
public class timer {
public static void main(String[] args){
double sum = 1.1078087563769418E16;
double sec=0;
double min=0;
double hou=0;
double da=0;
double yea=0;
yea=sum/31536000;
int year = (int) yea;
sum=sum-year*31536000;
da=sum/86400;
int day = (int) da;
sum=sum-day*86400;
hou=sum/3600;
int hour = (int) hou;
sum=sum-hour*3600;
min=sum/60;
int minute = (int) min;
sum=sum-minute*60;
sec=sum;
int second = (int) sec;
sum=sum-second*60;
System.out.println(year+" "+day+" "+hour+" "+minute+" "+second);
}
}
当双重“sum”变得非常大时,输出结果会变得奇怪。例如,双重“day”变得比365还要大,这应该是不可能的。
有什么解决办法吗?