1. ## 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. >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;
}```

3. 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.

4. prelude: that code works? what about setting the flags? is that already defined somewhere in the std namespace?

5. >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.

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;
}```

6. >>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...

7. 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.

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?

8. 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...

9. [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. >Does your code do it?

>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.

11. 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. 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. 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. 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?