Solved

Rounding off values

Posted on 1998-09-21
2
161 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 50 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now