🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #18228

Re: trouble with round

#18228
From: Rich Downing
Date: Mon, 20 Jul 1998 00:00
44 lines
2218 bytes
Dave Frizzell <davef@jtechmed.com> wrote:

>new to java, i have a double that i need to round to the hundredths
>place (0.01).
>
>double examPercent;
>examPercent = (totalGradePoints / max_points)* MAX_PERCENTAGE;
>round (examPercent); //??? this is very wrong
>
>do i want to use round or something else?  could someone be so kind as
>to help me with the correct syntax.
>
>thanks,
>davef

First of all, might I suggest that you always perform your
multiplications before performing division.

totalGradePoints/max_points will probably result in some roundoff or
precision error, which error will then be multiplied by
MAX_PERCENTAGE. This type of error becomes minimized if your change
your statement to  (totalGradePoints*MAX_PERCENTAGE) / max_points

To round to the second decimal you can use:

double examPercent = (double)(Math.round(
totalGradePoints*MAX_PERCENTAGE * 100 / max_points))  / 100;

Note that the following applies to normal "business rules" rounding.
In mathematics, rounding is done a bit differently.  Since the number
0 is not rounded; numbers 1,2,3,4,5 are rounded up, and numbers
6,7,8,9 are rounded down.  Hence one is rounding up 5 times and down 4
times.

To correct this one rounds up or down depending upon whether the
preceding digit was even or odd. (assuming random data).

Rich
----------------------------------------------------------------------
Richard V. Downing              Work email: richard.downing@vsin.com
Senior Software Engineer        Home email: rvd@mindspring.com
VideoConferencing Systems, Inc. Web: http://rvd.home.mindspring.com
Norcross, Ga
------------ When surrounded by wolves 'tis best to howl -------------
Technical Details
Message-ID: <35b35396.13036243@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <35B2DFEC.1F19@jtechmed.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,f5c385332d5757e
X-Google-Attributes: gidf74b3,public
From: rvd@mindspring.com (Rich Downing)
Subject: Re: trouble with round
Date: 1998/07/20
Message-ID: <35b35396.13036243@forums.borland.com>
X-Deja-AN: 373178491
Content-Transfer-Encoding: 7bit
References: <35B2DFEC.1F19@jtechmed.com>
Content-Type: text/plain; charset=us-ascii
Organization: mindspring.com
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language