C++ Question

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Is there a way to ensure that all values that are assigned to a certain variable are rounded off to the 10th decimal place?
 

javathehut

Senior member
Oct 23, 2000
318
0
0
What data type are you using? Float or double or something else? I would imagine that you would have to use the modulo function for this case. Maybe you would want to mulitply the var by 10x10 and convert it to int (to cut off the remainder) and then divide by 10x10 and convert it into a float. But thats ineffecient, I dont think that you would want to do that. My best recommendation would be to play around with the modulo function (%).
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
Hey, to convert a double varaiable say x, I thought you just had to write: int(x);

For some reason, I'm getting errors when trying this.
 

gittyup

Diamond Member
Nov 7, 2000
5,036
0
0
int varI = 0;
double varD = 2.57;

varI = int( varD );



edit >> woops double post during high traffic time..
 

Moohooya

Senior member
Oct 10, 1999
677
0
0
Depending upon the order of magnitude of your number, 10 decimal places is pushing it.

Try something like

double d = f();
d = floor (d * 1.0e10) / 1.0e10;

But it isn't floor. It might be floorl, I forget. (floor is for float not double)

Moohoo
 

Mears

Platinum Member
Mar 9, 2000
2,095
1
81
I tried * by 10e10 and then converting to int and then double and then finally dividing by 10e10, but the == operator still is considering them not equal. I'm starting to get pissed off. When I use cout statements and show the variables directly before they are evaluated they print off as being equal.