英文:
Why casting give me these weird results Java?
问题
以下是您要求的代码部分的翻译:
public class Main {
public static void main(String[] args) {
double x=5.6556464566546546546556465465465;
float y=(float)x;
double z= 1+y;
System.out.println(x+"\n"+y+"\n"+z);
}
}
以下是您要求的输出部分的翻译:
5.6556464566546545
5.6556463
6.655646324157715
如果您有其他问题或需要进一步帮助,请随时告诉我。
英文:
This is the code
public static void main(String[] args) {
double x=5.6556464566546546546556465465465;
float y=(float)x;
double z= 1+y;
System.out.println(x+"\n"+y+"\n"+z);
}
}
and this is the output
5.6556464566546545
5.6556463
6.655646324157715
I can understand the value of x and y but z from where it got those fractional numbers after the 3??!
Thank you very much
答案1
得分: 1
浮点数是Java中实际数字的近似表示,因为它们存储的方式不同。如果您需要精确的值,请使用BigDecimal。
英文:
Floats are an approximation of the actual number in Java, due to the way they're stored. If you need exact values, use a BigDecimal instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论