Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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


 
Reply With Quote
 
 
 
 
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
threads up.

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);
 
Reply With Quote
 
 
 
 
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" <> 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
>
>



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-16-2004
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.
 
Reply With Quote
 
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" <look-> 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.



 
Reply With Quote
 
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" <> wrote in message
news:F%Tpc.42621$...
> 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" <look-> 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.

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding any number (int or float) to 3 significant figures Max Williams Ruby 6 05-16-2009 01:25 PM
most significant and less significant address a01lida VHDL 2 11-16-2008 12:28 PM
Significant Digits (Significant Figures) SMH Javascript 0 01-07-2007 09:52 AM
Re: Round to significant figures (C++) Alf P. Steinbach C++ 0 05-01-2006 09:15 AM
Output to N significant figures, fixed-point and exponential Dr John Stockton Javascript 0 07-17-2005 06:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57