Hey guys sorry for the slightly vague title. I need help with something. I'm learning basic Java and I've been rolling a personal quandary through my head all day. In a situation where:
double x = 10;
How do I display x as an integer? I understand I could simply say:
System.out.println(+ (int)x);
But this is only temporary and only covers a single value of x. If the value of x is changed to 10.5, for example, than x will be displayed as 10. In situations where double x is in fact a decimal, I'd like it to display as a decimal. In situations where x is an integer (whole number) or perhaps where the value of x minus (int)x is less than or equal to say .01, then I'd like x to display as an integer. I've been playing with if, then statements, but nothing I can come up with, with my very limited knowledge, seems to work. I figured out a way where in situations where x - (int)x (this has been set to int y) is less than .01, x is displayed as a double, yet a whole number. To explain, if x = 1.001, then x is displayed as 1.0. However this doesn't really help me. I would really appreciate help for this.
Thank you.
double x = 10;
How do I display x as an integer? I understand I could simply say:
System.out.println(+ (int)x);
But this is only temporary and only covers a single value of x. If the value of x is changed to 10.5, for example, than x will be displayed as 10. In situations where double x is in fact a decimal, I'd like it to display as a decimal. In situations where x is an integer (whole number) or perhaps where the value of x minus (int)x is less than or equal to say .01, then I'd like x to display as an integer. I've been playing with if, then statements, but nothing I can come up with, with my very limited knowledge, seems to work. I figured out a way where in situations where x - (int)x (this has been set to int y) is less than .01, x is displayed as a double, yet a whole number. To explain, if x = 1.001, then x is displayed as 1.0. However this doesn't really help me. I would really appreciate help for this.
Thank you.