Re: double with precision
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Wed, 27 Jun 2007 18:02:36 -0400
Roy Gourgi wrote On 06/27/07 17:49,:
Hi,
I would like to save my double variable with a precision of 2 decimal
places.
For example the double x = 1.7358
I would like x = 1.73 or 1.74 (rounded)
How would I do that.
The best you're likely to do is something like
x = Math.round(x * 100.0) / 100.0;
.... which in this case will give a value that is very
close to 1.74. (It cannot be exactly 1.74, because 1.74
is not a number that can be represented exactly by a
double.)
HOWEVER, if the reason you want to round the value
is because you want to print it with two decimal places
instead of with four (or whatever), this is probably
the wrong thing to do. Instead, leave x as it is and
use a java.text.NumberFormat object to control the way
its value is converted to a string.
--
Eric.Sosman@xxxxxxx
.
- References:
- double with precision
- From: Roy Gourgi
- double with precision
- Prev by Date: double with precision
- Next by Date: Re: double with precision
- Previous by thread: double with precision
- Next by thread: Re: double with precision
- Index(es):
Relevant Pages
|