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



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/

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

    JacksMagicBean June 27, 2020 4:44 am Reply
  • This doesn't work

    Tiosh June 27, 2020 4:44 am Reply
  • thank You

    Sandupa Egodage June 27, 2020 4:44 am Reply
  • This was very helpful. Thank you!

    Koo Kar Wai June 27, 2020 4:44 am Reply
  • thanks

    Professor Squid June 27, 2020 4:44 am Reply
  • thankyou

    Hybrid Gaming June 27, 2020 4:44 am Reply
  • BROOOOO, tytytytyty

    Nicholas Mercer June 27, 2020 4:44 am Reply
  • Math.round( value * 1000) / 1000.0 saved my ass for an assignment. Thank you!

    Anhjje June 27, 2020 4:44 am Reply
  • you…….are ….. god……

    Your Lover June 27, 2020 4:44 am Reply
  • You can also simply do casting :

    double number = 3.141592653;

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

    System.out.println(rounded);

    3.14

    Sodbuster June 27, 2020 4:44 am Reply
  • thanks

    SimonSays June 27, 2020 4:44 am Reply
  • This help me so much THANK YOU !

    Jesse Gonzalez June 27, 2020 4:44 am Reply
  • Hey, nice 😀 Thanks, man!

    Mr. Trashcan June 27, 2020 4:44 am Reply
  • The rounding part saved me. Thank you!

    Skrublaub June 27, 2020 4:44 am Reply
  • Thank you very much kind sir. I'm a beginner and this helped me out a lot. Thank you!

    Leo June 27, 2020 4:44 am Reply
  • Thank you hero! Could you please tell me how to make user input to be 2 decimal??

    Liyuan Boo June 27, 2020 4:44 am Reply
  • Wow, finally someone explains this in a succinct, but also very clear way! Thank you!

    mladboy June 27, 2020 4:44 am Reply
  • didnt know badger from breaking bad taught java

    Patrick Gatt June 27, 2020 4:44 am Reply
  • Really helpful.Thanks man.

    Ahnaf Khan June 27, 2020 4:44 am Reply
  • thank you this really helped!

    Kierstyn June 27, 2020 4:44 am Reply
  • dude sounds like he wants to kill himself

    To Release is To Resolve June 27, 2020 4:44 am Reply
  • Thanks.

    Fezile Nkuna June 27, 2020 4:44 am Reply
  • THANK YOU!!!!

    Dr Gjengdahl June 27, 2020 4:44 am Reply
  • Thank you. It really helped me!

    Angel Peralta June 27, 2020 4:44 am Reply
  • Thanks a lot !!! It very useful to me !!!!!!

    俊宇 June 27, 2020 4:44 am Reply
  • I found the documentation for this function confusing. Now I understand. Thank you!

    Jacob French June 27, 2020 4:44 am Reply
  • Muy bueno gracias!!!!

    #LunaTutoriales June 27, 2020 4:44 am Reply
  • Thank you!!! Remember for the formatting, it is important to have "%.2f", number. The comma is very important, do not put +

    Phuong Huynh June 27, 2020 4:44 am Reply
  • This is the most helpful thing I've ever seen. Quick, concise and I love your voice (it makes it more enjoyable).

    Christian's Grocery Vlogs June 27, 2020 4:44 am Reply
  • This video was very helpful. I was looking for so long for this… so simple yet so hard to find. 🙂

    Christian D June 27, 2020 4:44 am Reply
  • YOu are just a life save, thank you

    Sandy San June 27, 2020 4:44 am Reply
  • 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

    Finn Grant June 27, 2020 4:44 am Reply
  • 1:05 …

    Smotteh June 27, 2020 4:44 am Reply
  • Thanks

    Void June 27, 2020 4:44 am Reply
  • really helpful! Thank you very much!

    Ting Wang June 27, 2020 4:44 am Reply
  • Great Explanation. Thanks!

    nsk8ter524 June 27, 2020 4:44 am Reply
  • Well done!

    Clayton Wahlstrom June 27, 2020 4:44 am Reply
  • It Works. Gracias Amigo!

    Andres Lizondro June 27, 2020 4:44 am Reply
  • Awesome video.  Thanks!

    Michael Bridges June 27, 2020 4:44 am Reply
  • or when you multiply, you could multiply by .100…..
    impressive right.
    im a freaking genius

    Will hand June 27, 2020 4:44 am Reply
  • Try zooming in next time, useless video

    bro1029384756 June 27, 2020 4:44 am Reply
  • Thank you so much 🙂

    YoungRace June 27, 2020 4:44 am Reply
  • Excellent video. Thanks

    Afif Khaja June 27, 2020 4:44 am Reply
  • Thanks man, this was the video I was looking for.

    rvbCabooservb June 27, 2020 4:44 am Reply

Leave a Reply

Your email address will not be published. Required fields are marked *