Solved

Rounding off values

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

766 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