How to round off a two decimal number to the nearest number? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 8

How to round off a two decimal number to the nearest number?

67.12 =67 And. 52.51 =53 and not 52

18th Dec 2019, 5:30 AM
Justin
Justin - avatar
3 Antworten
+ 6
double a = 45.67; System.out.println((int)Math.round(a));
18th Dec 2019, 7:57 AM
Avinesh
Avinesh - avatar
+ 2
import the math class to your project using the next line of code. import java.lang.Math; You can then round variable type double like you describe. the next line is an example where x holds the value you want to round. System.out.println(Math.round(x));
19th Dec 2019, 4:05 AM
Bernie Booth Jr
Bernie Booth Jr - avatar
+ 1
It’s been a while every since I did rounding back in my days but I’ll try to help you: Round the number n to p decimal places by first shifting the decimal point in n by p places by multiplying n by 10ᵖ (10 raised to the p th power) to get a new number m . Then look at the digit d in the first decimal place of m . If d is less than 5, round m down to the nearest integer. Otherwise, round m up. Just like Real life, check the number on your right (I mean the digits) if that digit on the right is 5 or higher then round it to the nearest 10s. Example: 38 rounded to the nearest ten is 40. Hope this helps 👌😄
19th Dec 2019, 4:48 PM
Wesl_y
Wesl_y - avatar