GitHub repo with examples

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:

1) Using Math.round()

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.

Inside the parentheses of the round method, multiply your number by a power of 10. Then outside the parentheses, divide by the same power of 10. The number of zeros after the ‘1’ is the number of decimals you will have. For example: rounding to two decimals looks like this: Math.round( number * 100)/100.0; ONE OF THE NUMBERS MUST HAVE .0 AFTER IT OR JAVA WILL PERFORM INTEGER DIVISION!

2) Using System.out.printf()

The printf method is similar to print, but allows you to format the variables. The ‘f’ in ‘printf’ stands for format. To print a variable rounded to 2 decimal places the code is System.out.printf( “%.2f”, number); The % symbol tells java that what comes next is a formatted variable. The The %f is replaced with the ‘number’ variables and the ‘.2’ tells Java to display 2 decimals. Change the number after the period to the number of decimals you want.

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

The DecimalFormat class must be imported by the statement: import java.text.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.

To round the variable, type: dFormatter.format(number) This uses the format() method of the DecimalFormat class to display the number the way you specified.

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.

Download TextPad, you can use it for free

Nguồn: https://rmacct.org/

Xem thêm bài viết khác: https://rmacct.org/cong-nghe/

### Xem thêm Bài Viết:

- Linh kiện “đáng gờm” xe máy điện VinFast Klara S 2022 thế hệ mới
- Đánh giá nhanh bo mạch chủ ASROCK X470 TAICHI ULTIMATE
- Cách tắt ứng dụng chạy ngầm android samsung, oppo, xiaomi, realme
- Không thể phá khối EXPLODE được Block trong autocad // Lệnh X (xplode) // Mẹo vặt autocad
- The RX 580 Is Still An AMAZING GPU For Mining Today!

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

This doesn't work

thank You

This was very helpful. Thank you!

thanks

thankyou

BROOOOO, tytytytyty

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

you…….are ….. god……

You can also simply do casting :

double number = 3.141592653;

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

System.out.println(rounded);

3.14

thanks

This help me so much THANK YOU !

Hey, nice 😀 Thanks, man!

The rounding part saved me. Thank you!

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

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

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

didnt know badger from breaking bad taught java

Really helpful.Thanks man.

thank you this really helped!

dude sounds like he wants to kill himself

Thanks.

THANK YOU!!!!

Thank you. It really helped me!

Thanks a lot !!! It very useful to me !!!!!!

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

Muy bueno gracias!!!!

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

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

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

YOu are just a life save, thank you

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

1:05 …

Thanks

really helpful! Thank you very much！

Great Explanation. Thanks!

Well done!

It Works. Gracias Amigo!

Awesome video. Thanks!

or when you multiply, you could multiply by .100…..

impressive right.

im a freaking genius

Try zooming in next time, useless video

Thank you so much 🙂

Excellent video. Thanks

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