I need to perform division between integers in Java, and the result should be a float.
Could I just use / symbol for it? As in:
int integer1 = 1; int integer2 = 2; float quotient = integer1 / integer2; // Could I do this? Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack InternalI need to perform division between integers in Java, and the result should be a float.
Could I just use / symbol for it? As in:
int integer1 = 1; int integer2 = 2; float quotient = integer1 / integer2; // Could I do this?
doubleinstead offloatasfloatcannot represent allintvalues accurately. try(double) integer1 / integer2doubleis merely less bad thanfloat. It doesn't solve the problem. I presume you already knew that, but I just wanted it to be on the record. :)intvalues without error. It cannot represent allint/intvalues without error, but there is likely to be approriate rounding which is acceptible for the OPs applications. Otherwise you need to use a Faction (for unlimited precision) or BigDecimal (for very long precision)