Velocity Reviews > Java > round up to nearest number and significant figures

# round up to nearest number and significant figures

Steve
Guest
Posts: n/a

 05-16-2004
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

Thomas Schodt
Guest
Posts: n/a

 05-16-2004
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);

Fred
Guest
Posts: n/a

 05-16-2004
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" <(E-Mail Removed)> wrote in message
news:e0Hpc.41550\$(E-Mail Removed)...
> 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
>
>

Roedy Green
Guest
Posts: n/a

 05-16-2004
On Sun, 16 May 2004 10:03:54 GMT, "Steve" <(E-Mail Removed)> 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
Guest
Posts: n/a

 05-17-2004
Thank you, I read your web page.

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Sun, 16 May 2004 10:03:54 GMT, "Steve" <(E-Mail Removed)> 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
Guest
Posts: n/a

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

"Steve" <(E-Mail Removed)> wrote in message
news:F%Tpc.42621\$(E-Mail Removed)...
> Thank you, I read your web page.
>
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On Sun, 16 May 2004 10:03:54 GMT, "Steve" <(E-Mail Removed)> 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.

>
>