Java: Rounding Numbers (Math.round(), DecimalFormat & printf)

preview_player
Показать описание
Java enables you to do almost anything, especially tasks involving numbers. But sometimes complicated calculations give you an answer with way too many decimals.
Most practical applications only require a few decimals. Here are 3 ways to round number in Java:

Java's Math class is inherently included in every program you create so there is not need for an import statement. The round() method takes a number as an argument and rounds that number to the nearest integer. On it's own this isn't spectacularly helpful but you can combine it with an easy little trick.

NOTE: The printf method doesn't change the actual contents of the variable. After using printf, the variable still has all the decimals.

3) Using DecimalFormat
Then create a reference variable to a DecimalFormat object: DecimalFormat dFormatter and set it equal to: new DecimalFormat(); In the parentheses, place "0.00" or "#.##" to round to 2 decimals. Add more zeros of ##'s after the period to change the number of decimals.
DecimalFormat formats variables as strings, so if you want to use a number for calculation you have to parse it back to double or float.

Рекомендации по теме
Комментарии
Автор

What took me 30 minutes of fumbling through the internet, took you 4 minutes to explain. Thank you!

TheDelaneyCurry
Автор

Thank you!!! Remember for the formatting, it is important to have "%.2f", number. The comma is very important, do not put +

phuonghuynh
Автор

This is the most helpful thing I've ever seen. Quick, concise and I love your voice (it makes it more enjoyable).

christiansgroceryvlogs
Автор

Wow, finally someone explains this in a succinct, but also very clear way! Thank you!

mladboy
Автор

BRO I love this. Straight to the point. I watched a 12 min video that left me with more questions than it answered. And yet you did in the first minute 🙏🙏🙏🙏

josealbertofernandez
Автор

Math.round( value * 1000) / 1000.0 saved my ass for an assignment. Thank you!

Anhjje
Автор

very helpful, thankyou. Ive been coding nearly half a year now and I stupidly forgot how to round to specified decimal places. I was midway through my program and realized I forgot one of the first things I learned

finngrant
Автор

I found the documentation for this function confusing. Now I understand. Thank you!

jacob
Автор

life saver for my lab due in an hour and 39 minutes!!

sktmx
Автор

best video about these methods, I wish I would've found this much sooner. Thank you!!

clea
Автор

The rounding part saved me. Thank you!

skrublaub
Автор

This video was very helpful. I was looking for so long for this... so simple yet so hard to find. :)

christiand
Автор

You can also simply do casting :


double number = 3.141592653;

double rounded = (int) (number * 100) / 100.0;


System.out.println(rounded);


3.14

BennyElie
Автор

Helped me finish a project in time. Thank you sir!

wcsdiaries
Автор

Another method I found:
System.out.println(String.format("%.xf", d)); Where x is the # of decimal places you want and d is a double or integer you're working with.

sportoman
Автор

Thank you so much! I love this trick!

AmongUs-qtuo
Автор

Thank you very much kind sir. I'm a beginner and this helped me out a lot. Thank you!

Bandgeek
Автор

Could you help me out? I need to cut my number without rounding it. And your method rounds the number. For example the number 21.4567 should be shown an 21.45 and not as 21.46, which method could I use? Or what should I do generally?

minus
Автор

Thanks man, this was the video I was looking for.

rvbCabooservb
Автор

Thank you hero! Could you please tell me how to make user input to be 2 decimal??

cutieboo