Click to See Complete Forum and Search --> : Round a variable in double type.


yabing
05-02-2001, 01:59 AM
How to round a double type number correct to a specified number of decimal
places.
Thanks.
yabing

jonnin
05-02-2001, 06:30 AM
multiply my 10, 100, whatever so that the digit you want to round to is in
the ones place. If negative , subtrace .5; if positive, add .5. Take int
of it into the double (d = (int)d) to truncate the extra places. Move back
by dividing by 10,100, etc.

1234.567 //number
12345.67 //*10
12346.17 //+.5
12346.00 //int
1234.6 //rounded double


-12.34 //neg
-123.4 //*10
-123.9 //-.5
-123.0 //int
-12.3 //double


there may be a more efficient way, but thats the idea.

"yabing" <yabing@manres.com.au> wrote:
>
>
>How to round a double type number correct to a specified number of decimal
>places.
>Thanks.
>yabing
>