Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Java

In java How to write program to round off decimal(Float , double) to nearest int value, Without using any method Anybody have idea plz tell me

26th Feb 2020, 11:30 AM
Haritha Vuppula
Haritha Vuppula - avatar
6 Answers
+ 3
Math.round -> round to nearest value. eg- 5.3 to 5.0 and 5.7 to 6.0 Math.floor -> round to nearest lower limit. eg- 5.6 to 5.0 Math.ceil -> round to nearest upper limit. eg- 5.2 to 6.0
26th Feb 2020, 1:46 PM
Avinesh
Avinesh - avatar
+ 2
To understand what you need more, Can you share your try? Edit: double a=3.45; a=a-a%1; //floor a=(a-a%1)+1; //ceil
26th Feb 2020, 12:37 PM
Jayakrishna 🇮🇳
0
Put some efforts first. Share your code here and we will help you out in solving the errors. Hint: try type casting.
26th Feb 2020, 11:42 AM
Chetali Shah
Chetali Shah - avatar
0
i had put i solved using type casting then Math.round nd then Math.floor i jus want differentiation
26th Feb 2020, 12:04 PM
Haritha Vuppula
Haritha Vuppula - avatar
0
"Without using any method" so you can't use Math functions (type casting) is good way, +1 if dec%1>=0.5 then solve negative sign for -2.55 -> -3.0
26th Feb 2020, 6:03 PM
zemiak
0
thnq
26th Feb 2020, 6:28 PM
Haritha Vuppula
Haritha Vuppula - avatar