Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# double "rounding"

Ranch Hand
Posts: 74
I have two questins related:
* First of all, I wanted to know if there is any function which rounds a double number to a double with a concrete number of decimals. For example, if I have the number 82.0716987 and I want to round it to two decimals I should get 8.,07. does it exist that function??

*As, by now, I didn't find a function which do that, I decided to implement my own like this:
double aux=result*100;
double mydouble=82.0716987;
double aux=mydouble*100;
int auxInt = (int)aux;
aux = aux-auxInt;
aux=aux/100;
mydouble=mydouble-aux;
return mydouble;
And if the double number has a lot of decimal it works, but if not it doesnt work. for example, in this case it works like this:
double mydouble=82.0716987;
double aux=mydouble*100; //aux=8207.16987
int auxInt = (int)aux; //auxInt=8207
aux = aux-auxInt;//aux= 0.16986999999971886 !!!
aux=aux/100;
mydouble=mydouble-aux;
return mydouble;
How is it posible that if I do operate 8207.16987-8207 i can get 0.16986999999971886 ??? How can I solve it??
Thank-you very much for your help,

Joyce Lee
Ranch Hand
Posts: 1392

Try out the following code:

Joyce
[ September 11, 2004: Message edited by: Joyce Lee ]

Ranch Hand
Posts: 74
That is great, but I still have a problem. I have to pass the result (82.07) as a double, not as a String, and it doesn�t let me convert the result because of the ",". I could replace the ',' by a '.', then create a Double an then get the doublevalue(), but maybe you know a shorter way.
Anyway, thank-you very much four help, it is really useful

Joyce Lee
Ranch Hand
Posts: 1392
The output for f.format(82.0716987) is 82.07 instead of 82.,07, no comma. To convert double to String, you don't have to create a Double instance.

Joyce
[ September 11, 2004: Message edited by: Joyce Lee ]

Ranch Hand
Posts: 74
I still have the same problem (with the ',') I am just using the lines you provided me and the format operaton returns 82,16 instead of 82.16

Joyce Lee
Ranch Hand
Posts: 1392
I still have the same problem (with the ',') I am just using the lines you provided me and the format operaton returns 82,16 instead of 82.16

I couldn't duplicate the comma output on my PC unless I specifically modified the symbol. Here is I've done:

You can also refer to Sun Tutorial on Customizing Format for information on how to manipulate the formatter and symbol.

Hope that helps.

Joyce
[ September 11, 2004: Message edited by: Joyce Lee ]

Julian Kennedy
Ranch Hand
Posts: 823
It's your default Locale. I assume that "," is the natural decimal separator where you're from. If I do this:

the output I get is "82,07" as "," is what the French use as a decimal point. If you do the same and set Locale.ENGLISH or UK or US then you should get what you want.

On the other hand you might be better off doing something like the following:

Hope that helps.

Jules