is there a possibility that casting a double created via
Math.round()will still result in a truncated down number
No, round() will always round your double to the correct value, and then, it will be cast to an long which will truncate any decimal places. But after rounding, there will not be any fractional parts remaining.
Here are the docs from Math.round(double):
Answer from jjnguy on Stack OverflowReturns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)
is there a possibility that casting a double created via
Math.round()will still result in a truncated down number
No, round() will always round your double to the correct value, and then, it will be cast to an long which will truncate any decimal places. But after rounding, there will not be any fractional parts remaining.
Here are the docs from Math.round(double):
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)
For the datatype Double to int, you can use the following:
Double double = 5.00;
int integer = double.intValue();
This truncates (floors), and does not round. Use at your own risk.
Videos
I tried to cast it but the value still cant be converted. int quarter = (int)Monetary/0.25;//monetary is a double value it says there is an incompatible types error. Pls help me.
In my code I try to convert the double that a person would input into the console to an integer but I don't understand why it would not happen
Code is here https://onlinegdb.com/eILtFt5gg