Thread: Truncating a double?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    Truncating a double?

    Is there an easy way to truncate a double like 12.134 to 12.13? I'm not talking about on screen, I know that can be accomplished by the stuff in setiosflags. I'm talking about storing it as two places, and rounding up if the thousandths (.001) is five or higher. I am writing a program that computes the price for an item based on an input file, and when it asks for what the customer paid, 12.13 comes up as being insufficient to pay for a bill that is 12.134... Hopefully this is making some sense... In our specifications it says that this one cent discrepency may be ignored, but since I have some extra time to work on it I want to take care of that as well. Is truncation or rounding acceptable? If so how can I round or truncate based on that thousandths digit? Thanks in advance.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm talking about storing it as two places
    You can't change how floating-point values are represented. You can change how they are printed and how much of the representation is used in your program. In your case, all you have to do is set the precision and cout will handle the rounding for you:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
      double d1 = 12.135;
      double d2 = 12.134;
    
      cout<< setprecision ( 4 ) << d1 <<endl;
      cout<< setprecision ( 4 ) << d2 <<endl;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    I knew setprecision (2) would show the two places, but it doesn't allow for me to set 12.134 or whatever equal to 12.13. I guess that I'll just leave it as it is. Oh well... thanks anyway.
    Last edited by criticalerror; 12-08-2003 at 10:30 AM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    prelude: that code works? what about setting the flags? is that already defined somewhere in the std namespace?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Oh well... thanks anyway.
    Did you even bother to read and test the code I posted? It isn't there just to make my post longer you know.

    >that code works?
    I prefer not to post code that doesn't.

    >what about setting the flags?
    For greater control you should use the fixed modifier:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
      double d1 = 12.135;
      double d2 = 12.134;
    
      cout<< cout.precision ( 2 ) <<endl;
      cout<< fixed << d1 <<endl;
      cout<< fixed << d2 <<endl;
      cout<< cout.precision() <<endl;
    }
    My best code is written with the delete key.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    >>I prefer not to post code that doesn't.
    I kinda figured that it did work, I'm just not used to seing the cout.setf(blah blah) code...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by Prelude
    >Oh well... thanks anyway.
    Did you even bother to read and test the code I posted? It isn't there just to make my post longer you know.

    >that code works?
    I prefer not to post code that doesn't.

    >what about setting the flags?
    For greater control you should use the fixed modifier:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
      double d1 = 12.135;
      double d2 = 12.134;
    
      cout<< cout.precision ( 2 ) <<endl;
      cout<< fixed << d1 <<endl;
      cout<< fixed << d2 <<endl;
      cout<< cout.precision() <<endl;
    }
    What the original poster wants to accomplish is that when he compares d1 to 12.13, they will be equal. Does your code do it?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    something liek this should work:
    Code:
    ...
    double D;
    ... //something goes into D
    D=static_cast<int>(D*100)/100.0;
    ...
    that sets 12.345678 to 1234.567890 and then casts it as an int which would truncate it to 1234, then divides it by 100.0 to make it 12.3400000. use 100.0 instead of 100 to ensure that it doesn't truncate it again to 12. actually, the value would probably be 12.34xxxxx where the x's are garbage... computers sometimes have a had time with that little accuracy thing...

    I'm not 100% sure about that though, you may want to test it out...

    In our specifications it says that this one cent discrepency may be ignored, but since I have some extra time to work on it I want to take care of that as well. Is truncation or rounding acceptable?
    that one cent discrepency is probably meant to allow you to truncate the number (because rounding is more involved), but if you truncate, you run the risk of it being two cents off... example:

    given: 12.345
    accepted: 12.35
    truncated to: 12.34
    computer's error (-.01): 12.33

    your now two cents off. with my experience and floats, my computer was off by about +/- .01 every 20 or so calculations... it's intermittent enough so that it may slip by whoever's reading your program, and they probably won't notice if they don't do in depth testing.

    ^ don't listen to that last paragraph... it's bad ethic...
    Last edited by major_small; 12-08-2003 at 11:06 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    [QUOTE]Originally posted by Prelude
    >Oh well... thanks anyway.
    Did you even bother to read and test the code I posted? It isn't there just to make my post longer you know.


    The code displayed D1 as 12.14 and D2 as 12.13. But d2 is still 12.134 and d1 is 12.135 in the memory, correct? this doesn't change their values where they are stored in memory, just on screen.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does your code do it?
    Do you always ask questions that you know the answer to?

    >What the original poster wants to accomplish is that when he compares d1 to 12.13, they will be equal.
    Testing floating-point values for equality is more difficult than with integers.
    Code:
    #include <iostream>
    #include <cfloat>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
      double d1 = 12.134;
    
      if ( fabs ( d1 - 12.13 ) <= 12.13 * DBL_EPSILON )
        cout<<"First test: Equal"<<endl;
      d1 = 12.13;
      if ( fabs ( d1 - 12.13 ) <= 12.13 * DBL_EPSILON )
        cout<<"Second Test: Equal"<<endl;
    }
    >this doesn't change their values where they are stored in memory
    I said in my first reply that you cannot change how the values are stored in memory. I'm sorry for misreading your post.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    I'm sorry, I forgot that floating points couldn't really be counted on as being accurate, I thought maybe there was a way to get rid of the digits I didn't want... Thanks.

  12. #12
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    At the risk of incurring several different wraths at once...... Woohoo!!! code war! 's the clash of some titans.. Baby.
    Sorry lil' excited there. Anyway to settle all this before it goes farther (If it's not), Prelude's right, I haven't read anywhere where you can change how floats are stored in memory, but you can try some nifty tricks which'll compare floats of equal length e.g. after the two places everything's zeroed out; that sorta thing) like one of the replies

  13. #13
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    that's what I was going for... comparing the floats of a certain length... comparing only to the .01 spot... nothing farther, because for this assignment it is all I need.

  14. #14
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by criticalerror
    that's what I was going for... comparing the floats of a certain length... comparing only to the .01 spot... nothing farther, because for this assignment it is all I need.
    >I'm sorry, I forgot that floating points couldn't really be counted on as being accurate, I thought maybe there was a way to get rid of the digits I didn't want... Thanks.
    Just checking, you've found the solution already in this thread, haven't you?
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM