In java, when printing a double, how do I keep it from rounding up?
You have to assign the double data type instead of int(integer). Example, double number = 2.023;
Yeah. Say I have: double round = 2.446; System.out.printf("Round: %.02f", round); this prints 2.45 I don't want it to round.
I'm just wondering out of curiosity, but why do you want to do that? 2.446 is closer to 2.45 than 2.44, isn't it more appropriate to just let it round normally? Anyway, what you want to do is a rather unusual and there are therefore not any simple way (I known of) to do this. It's not impossible, but you'll probably have to write your own rounding method. http://stackoverflow.com/questions/8486878/how-to-cut-off-decimal-in-java-without-rounding
Just for kicks, I wrote a program for you. In short, it will take any number as an input, and if there is more than 2 decimal places, it will cut them all off... so no rounding. I did this using string manipulation, and by getting the position of the decimal point using the charAt method in a for loop; this way, I can 'not round' the input to 2 decimal places regardless of the input's size. Enjoy- Woodrow
If you are truly new to java & you want to copy my program onto your IDE, make sure that you change the name of the class, so that it's the same as the name of your program.
Join our real-time social learning platform and learn together with your friends!