# setprecision??

• 03-22-2006
rachael033
setprecision??
Hey,
I'm trying to use set precision but it only makes to answer of an equation come out wit no decimal places but I want a number to go into the equation with no decimal places.
Is there any way that you can put setprecision into an equation?
Thanks
• 03-22-2006
laserlight
Provide the smallest and simplest program (or code snippet) that demonstrates your problem.
• 03-22-2006
rachael033
h = round ((d/x * 48) /5);
n = round ((e/x * 48) /5);
cout << fixed << setprecision(0);
r = (h*5);
y = (n*5);

I need this to come out as multiple of 5 but it wont round the h and n when they are being multiplied by 5. It only rounds them if it stops before the multiply by 5 bit.
• 03-22-2006
SlyMaelstrom
Quote:

Originally Posted by rachael033
Hey,
I'm trying to use set precision but it only makes to answer of an equation come out wit no decimal places but I want a number to go into the equation with no decimal places.
Is there any way that you can put setprecision into an equation?
Thanks

setprecision does absolutely nothing to the data, and there for does nothing to equations that handle the data. All it does is format the output of floats. If you want the data to be rounded without decimals, then cast the float as an int.
• 03-22-2006
rachael033
But then if I do that when I compile it it has a warning that comes up saying warning - converting 'float' to 'int'. I want to try to avoid that.
• 03-22-2006
SlyMaelstrom
No, you wouldn't get a warning if you were typecasting it. I'd guess what you're doing is actually just assigning it to an int
Code:

float foo = 5.6;
int bar = foo;  // This produces a warning for the implicit cast
int baz = (int)foo;  // This doesn't produce a warning

// Now keeping in mind that this doesn't round, it just knocks off the decimal, then
// consider trying something like this:

int qux;
if (foo - (int)foo < .50)
qux = (int)foo;
else
qux = (int)foo + 1;

// Lastly, if you're not interested in creating a new int variable. Then you can
// use the ?: conditional operator in any statement, assuming you don't mind the clutter:

std::cout << (foo - (int)foo < .50 ? (int)foo : (int)foo + 1);