round up to nearest number and significant figures

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

  1. Steve

    Steve Guest

    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
    #1
    1. Advertising

  2. 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);
     
    Thomas Schodt, May 16, 2004
    #2
    1. Advertising

  3. Steve

    Fred Guest

    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
    #3
  4. Steve

    Roedy Green Guest

    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.
     
    Roedy Green, May 16, 2004
    #4
  5. Steve

    Steve Guest

    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" <> 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
    #5
  6. Steve

    Steve Guest

    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" <> 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
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Watts

    more than 16 significant figures

    Jeremy Watts, Jul 4, 2005, in forum: Java
    Replies:
    32
    Views:
    1,330
    Tom N
    Jul 15, 2005
  2. Alf P. Steinbach

    Re: Round to significant figures (C++)

    Alf P. Steinbach, May 1, 2006, in forum: C++
    Replies:
    0
    Views:
    736
    Alf P. Steinbach
    May 1, 2006
  3. Max Williams
    Replies:
    6
    Views:
    124
    Rob Biedenharn
    May 16, 2009
  4. Dr John Stockton
    Replies:
    0
    Views:
    122
    Dr John Stockton
    Jul 17, 2005
  5. SMH
    Replies:
    0
    Views:
    232
Loading...

Share This Page