For Programmers: Free Programming Magazines  


Home > Archive > Java Help > November 2005 > rounding numbers









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author rounding numbers
ste1986

2005-11-23, 9:57 pm

how do i round decimal numbers?

iv tried Math.round.rental but it wont work. i think im doing it wrong!!!

Hal Rosser

2005-11-23, 9:57 pm


"ste1986" <steiddon_1@hotmail.com> wrote in message
news:dbc8ef3628820f74e3f48f633a5458a8@lo
calhost.talkaboutprogramming.com...
> how do i round decimal numbers?
>
> iv tried Math.round.rental but it wont work. i think im doing it wrong!!!


TRY
long roundedResult = Math.round(rental)
>

Show us the code.
Math.round(yourNumber) returns a long if yourNumber is a double - but
returns an int if yourNumber is a float.
If you're wanting to round to a certain number of places, there are several
approaches.
1. Look at the NumberFormat class in the API -
OR
2. to round to 2 digits - multiply by 100 - then round to an int, then
divide by 100.


Roedy Green

2005-11-23, 9:57 pm

On Wed, 23 Nov 2005 16:22:19 -0500, "ste1986" <steiddon_1@hotmail.com>
wrote, quoted or indirectly quoted someone who said :

>how do i round decimal numbers?


To round a float or double, use Math.round or add .5 and convert to
int.

To round a BigDecmial use BigDecmial.round
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
cbroussard@liquiddatainc.com

2005-11-26, 9:57 pm

I'd also look at
http://java.sun.com/j2se/1.4.2/docs...igDecimal.html.

www.binaryfrost.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com