: : Dunno, maybe this is a stupid question, but I've had trouble finding anything about rounding numbers in basic C++. I'm sorta new at this so if yall could give me a bit of help on this. Anyways, later!
: :
: :
:
:
: Here is some code for a console app that should help:
:
: #include <stdio.h>
: #include <iostream.h>
:
: int main(int argc, char *argv[])
: {
: float f;
: float rounded;
: f = 2.71828182845904523536028747135266;
: rounded = (int)((f+0.005)[/size]*100); // rounded = 271
: cout << rounded;
: cout << '\n'; // break the line
: rounded /= 100; // rounded = 2.71
: cout << rounded;
: while (0==0) // keep the console open so you can see the outputs
: {}
: return 0;
: }
:
: The "(int)" means the result of the multiplication is truncated. It is a little different from rounding. With normal rounding, it would round up to 2.72 but instead the rest of the number is ignored.
:
:
:
If you would rather have the value rounded and not truncated, see red