Quick C++ Question.

Boomer151

Junior Member
Jun 25, 2001
12
0
0
say I have a float of 11.749. I have set persicon set to 2 so it would output as 11.74 but I need it to round that number up. I need 11.75 not 11.74 how is this done?
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
Unless there's a library out there that I'm not aware of, you'll have to manually do the math to get it to display the way you want it to.


atwl
 

manly

Lifer
Jan 25, 2000
13,024
3,781
136
I took a look in Stroustrup's C++ Programming Language, and according to Section 21.4.3,



<<
Note that floating-point values are rounded rather than just truncated and that precision() doesn't affect integer output.
>>



Maybe you're seeing a compiler bug.
 

Boomer151

Junior Member
Jun 25, 2001
12
0
0


<< I took a look in Stroustrup's C++ Programming Language, and according to Section 21.4.3,



<<
Note that floating-point values are rounded rather than just truncated and that precision() doesn't affect integer output.
>>



Maybe you're seeing a compiler bug.
>>



Damn that seems to be it.

If I compile it in Visual C++ is doessn't do the rounding, but when I uped it to my BSD machine and compiled it in g++ it works fine. Odd
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
If I compile it in Visual C++ is doessn't do the rounding, but when I uped it to my BSD machine and compiled it in g++ it works fine. Odd

Not really, VC++ is known to not follow a number of C++ standards.
 

manly

Lifer
Jan 25, 2000
13,024
3,781
136
Just as Nothinman said,

I was going to add, it should be called non Visual non C++
 

dishawbr

Member
Mar 2, 2002
69
0
0
You should try a little function that will test the bit pattern of the 3rd digit on the right of the decimal place. From there you can decide if you should round up or down..
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Just write a function to help you out. Here's a simple, unoptimized example:

float RoundMe(float num)
{
float multiplier = 100;
float new_num = num * multiplier;

if((float)(new_num - (int)new_num) >= 0.5)
new_num++;

return (float)((int)new_num / multiplier);
}

Nothingman, do you ever miss the chance to put down VC++?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Nothingman, do you ever miss the chance to put down VC++?

Of course not, I used it until I realized how bad it was, then later I found out how non-standards compliant it is, like most other MS products.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |