Re: double with precision



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
.



Relevant Pages

  • Re: Formating numbers
    ... Is there a way to control the precision at run time? ... The .NET formatting doesn't support formatting in the formatting, ... Using string operations to construct a format string is the way to do it. ...
    (microsoft.public.dotnet.general)
  • Integrating Own Control with VS Designer question?
    ... I have a property of a Control that makes it possible ... How do I in the designer view make the user have ... The property is a string right now.. ... type "Round" in there... ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Why (0.09+0.01-0.1) is not equal to 0.09+0.01-0.1 ?
    ... > gives full accuracy by default, but also includes rounding algorithms ... > partitioned mantissa by without risk of overflow ... > Function D2BAs String ... the work involved in avoiding lost precision due to type ...
    (microsoft.public.excel)
  • Re: Are f.p. manipulation functions only used in initialization?
    ... Easier IMO to look at Intels documentation such as ... as cos, sin, exp, log) if you change the FPU precision. ... You know that precision control only applies to addition, subtraction, ... having the FPU in double precision, ...
    (comp.lang.fortran)
  • Re: How to use FPU 80bit FP
    ... the precision bits in this newsgroup. ... INTEL FPU FLOATING POINT FORMATS ... FPU CONTROL WORD ... because stack faults should never be masked, ...
    (comp.lang.asm.x86)