# Solved Round Double to 2 decimals

Discussion in 'Spigot Plugin Development' started by Creepermanthe3rd, Apr 18, 2017.

1. ### Creepermanthe3rd

Hi, Im making a bank note plugin and when someone withdraws a number like /withdraw 10.5550054 it looks weird and shit because so I was wondering how I could round it down or just stop them having more then 2 decimals in general.
Thanks

2. ### FlyingLlama

subString(0,indexOf('.')+2)

• Optimistic x 1

Not a good way due to is not a string.

double d = 121.88324
double dr = Integer.parseInt(d * 100)/100

It's 100 due to you want it to have 2 decimals. So you get the integer value of that number and then you divide it into 100 (2 decimals, 1000 for 3 decimals, etc.)

• Agree x 1
4. ### Totom3

Hello,

There is a much better way of doing that in Java, using DecimalFormat.

Code (Text):
double number = [...];
DecimalFormat format = new DecimalFormat("0.00");

String output = format.format(number);
Why is this better? It's much more flexible and can be updated easily. For example, if you want to not show extra 0s (e.g. 5 rendering as "5" rather than "5.00"), simply replace "0.00" with "0.##". Furthermore, if you decide you want only 1 decimal, you use "0.0" or "0.#". Thirdly, the code is much more clear and straight-forward.

• Agree x 1
5. ### FlyingLlama

Yes, it is a string, when you go to print it, which is the only time when it matters?

WTF ofc is a string, but you can't convert it before printing it lol
And he said he want to round it, not to convert it

7. ### Creepah

@Creepermanthe3rd

Just a heads up, general programming questions like this can be googled and you'll receive many results a lot quicker than you would posting here. There are websites such as http://stackoverflow.com which are dedicated to questions such as these.

This way this forum can focus on more Minecraft-related queries.

• Like x 1
8. ### FlyingLlama

Um, of course you can. a String is a String. did you actually learn java before coming here? You print the converted string..

9. ### Synapz

@Creepermanthe3rd wants to round the decimal not truncate it. You cannot do any calculations like rounding while it is a String.

This does not work? Integer.parseInt() accepts a String not a double

I believe you confused it a little (unless it is because I learned it a different way)

This is the final result I came up and tested:
Code (Text):
double d = 121.88824;
double dr = (int) ((d * 100) + 0.5);
double finalValue = dr / 100;

System.out.println(finalValue);
It can be simplified. A little explication of basic rounding.:

You add 0.5 then turn it into an int to truncate it. For example, 1.8 + 0.5 is 2.3, turning to an int is 2. But, 1.3 +0.5 is 1.8, which turned into an int is just 1.

The problem with this is if you convert it to an int, you will lose the 2 decimal places. Instead, you have to first multiply by 100, do the rounding then bring it back to a decimal:

1. Multiple 121.88824 by 100 = 12188.824
2. Do the first step of rounding (add 0.5): 12188.824 + 0.5 = 12,189.324
3. Truncate it by converting to an int = 12189
4. Bring it back the two decimal places: 12189 / 100 = 121.89

#9
Last edited: Apr 19, 2017
• Agree x 1