?
Solved

Rounding off values

Posted on 1998-09-21
2
Medium Priority
?
172 Views
Last Modified: 2010-04-01
CString cs;
float Dummy = 48;
Dummy = ((100*Dummy)/322);
cs.Format("Dummy = %f ",Dummy);
m_static1.SetWindowText(cs);

How do I round off the float Dummy to 2 decimal places and loose all evidence of the previous unrounded number.
0
Comment
Question by:rose3377
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 1173225
1. TRUE ROUNDING
For sake of clarity on multiple lines (you can pack this into one equation).

Dummy = Dummy * 100 ;
Dummy = floor(Dummy + 0.5 ) ;
Dummy = Dummy / 100 ;

Do this before converting dummy to a string


or

2. ROUNDING FOR DISPLAY

change
cs.Format("Dummy = %f ",Dummy);
to
cs.Format("Dummy = %.2f ",Dummy);

0
 
LVL 2

Expert Comment

by:JYoungman
ID: 1173226
#include <math.h>
double round_two_places(double x)
{
    return ( floor(x*100.0 + 0.5) / 100.0 );
}
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question