# round up to nearest number and significant figures

Discussion in 'Java' started by Steve, May 16, 2004.

1. ### SteveGuest

1. Is there a way to convert a variable .09999999 to .1?

2. Likewise how can I convert 108229.99 to 108230.0 (it must alway be
rounded up and be an integer)

Thanks

Steve, May 16, 2004

2. ### Thomas SchodtGuest

Steve wrote:

> 1. Is there a way to convert a variable .09999999 to .1?

For a given precision .09999999 is equal to .1
A floating point value is not stored like that.

IIRC Roedy posted a link to an explanation of floating point only a few

Maybe you want BigDecimal?
Read what Roedy has to say.
<http://mindprod.com/jgloss/gotchas.html#BIGDECIMAL>

> 2. Likewise how can I convert 108229.99 to 108230.0 (it must alway be
> rounded up and be an integer)

double foo = 108229.99;
int bar = (int)(foo+.5);

Thomas Schodt, May 16, 2004

3. ### FredGuest

Use the DecimalFormat class - for example here is a snippet...

import java.text.*;

class test
{
public static void main(String Args[])
{

DecimalFormat fmtObj = new DecimalFormat("####0.00");

double d1 = 1.0199999;

System.out.println(d1);
System.out.println(fmtObj.format(d1));
}

}

Here are the results from running this class file:

1.0199999
1.02
Press any key to continue...

Hope this helps you,

Fred.

"Steve" <> wrote in message
news:e0Hpc.41550\$...
> 1. Is there a way to convert a variable .09999999 to .1?
>
> 2. Likewise how can I convert 108229.99 to 108230.0 (it must alway be
> rounded up and be an integer)
>
> Thanks
>
>

Fred, May 16, 2004
4. ### Roedy GreenGuest

On Sun, 16 May 2004 10:03:54 GMT, "Steve" <> wrote or quoted :

>1. Is there a way to convert a variable .09999999 to .1?
>
>2. Likewise how can I convert 108229.99 to 108230.0 (it must alway be
>rounded up and be an integer)

see http://mindprod.com/jgloss/floatingpoint.html

--
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Roedy Green, May 16, 2004
5. ### SteveGuest

I am just a bit confused how to use this information. The Math funtionality
is not recognised and I have almost no experience in java.

I presently have
float MPoriginalFormat = (MP2+MDC)*1000; //110060, could show as 110059.99

// need to convert this to 110060

Use long Math. round ( double )

Thank you.

"Roedy Green" <> wrote in message
news:...
> On Sun, 16 May 2004 10:03:54 GMT, "Steve" <> wrote or quoted :
>
> >1. Is there a way to convert a variable .09999999 to .1?
> >
> >2. Likewise how can I convert 108229.99 to 108230.0 (it must alway be
> >rounded up and be an integer)

>
> see http://mindprod.com/jgloss/floatingpoint.html
>
> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Steve, May 17, 2004
6. ### SteveGuest

got it, thanks
MPoriginalFormat2= (int) Math.ceil(MPoriginalFormat); //it needs to be an
integer rounded up

"Steve" <> wrote in message
news:F%Tpc.42621\$...
>
> I am just a bit confused how to use this information. The Math

funtionality
> is not recognised and I have almost no experience in java.
>
> I presently have
> float MPoriginalFormat = (MP2+MDC)*1000; //110060, could show as 110059.99
>
> // need to convert this to 110060
>
> Use long Math. round ( double )
>
> Thank you.
>
>
>
>
> "Roedy Green" <> wrote in message
> news:...
> > On Sun, 16 May 2004 10:03:54 GMT, "Steve" <> wrote or quoted :
> >
> > >1. Is there a way to convert a variable .09999999 to .1?
> > >
> > >2. Likewise how can I convert 108229.99 to 108230.0 (it must alway be
> > >rounded up and be an integer)

> >
> > see http://mindprod.com/jgloss/floatingpoint.html
> >
> > --
> > Canadian Mind Products, Roedy Green.
> > Coaching, problem solving, economical contract programming.
> > See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

>
>

Steve, May 17, 2004