Click here to Skip to main content
15,902,112 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Rounding double value Pin
Cedric Moonen25-Sep-07 21:02
Cedric Moonen25-Sep-07 21:02 
GeneralRe: Rounding double value Pin
Stephen Hewitt25-Sep-07 21:17
Stephen Hewitt25-Sep-07 21:17 
GeneralRe: Rounding double value Pin
Nelek25-Sep-07 21:54
protectorNelek25-Sep-07 21:54 
AnswerRe: Rounding double value Pin
Cedric Moonen25-Sep-07 20:55
Cedric Moonen25-Sep-07 20:55 
GeneralRe: Rounding double value Pin
KASR125-Sep-07 21:03
KASR125-Sep-07 21:03 
GeneralRe: Rounding double value Pin
Nishad S25-Sep-07 21:09
Nishad S25-Sep-07 21:09 
GeneralRe: Rounding double value Pin
Cedric Moonen25-Sep-07 21:13
Cedric Moonen25-Sep-07 21:13 
AnswerRe: Rounding double value [modified] Pin
Nelek25-Sep-07 21:51
protectorNelek25-Sep-07 21:51 
This is another way to do that.

I did it with VC++ 6.0 to be used in my project. The return value is a string to be shown in the GUI, but you can easily adapt it just for numbers.

CString CMyDoc::CutAndRoundStringToNDecimals (double dValue, const int nDec)
{	double dTemp = 0, dFract = 0, dInt = 0, dRes = 0;
	int nCount = 0;
	CString cLetter = "";

	// Operations to round up the nth decimal number if necessary
	dTemp = dValue*pow(10,nDec);
	dFract = modf (dTemp, &dInt);
	if (dFract >= 0.5)
		dInt++;
	dRes = dInt/pow(10,nDec);

	// Set the needed large of the string to be placed on the screen
	CString szResult;
	szResult.Format ("%s%f", szResult, dRes);
	int nLarge = szResult.GetLength ();
	for (int i = 0; i < nLarge; i++)
	{	nCount++;
		CString cLetter = szResult.GetAt (i);
		if (cLetter == ".")
		{	nCount += nDec;
			break;
		}
	}

	// Truncate the string to be placed in the screen
	LPTSTR pStr = szResult.GetBufferSetLength (nCount);
	szResult.ReleaseBuffer ();

	return szResult;
}


Hope it helps

I forgot to say that math.h has to be included to use pow () and modf()
-- modified at 11:41 Wednesday 26th September, 2007



Greetings.

--------
M.D.V.

If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

Help me to understand what I'm saying, and I'll explain it better to you

Wink | ;)

AnswerRe: Rounding double value Pin
beko25-Sep-07 23:57
beko25-Sep-07 23:57 
GeneralRe: Rounding double value Pin
KASR126-Sep-07 1:15
KASR126-Sep-07 1:15 
QuestionCan we post data on web site through VC++ application? Pin
megha_gharote25-Sep-07 20:42
megha_gharote25-Sep-07 20:42 
QuestionRe: Can we post data on web site through VC++ application? Pin
Hamid_RT25-Sep-07 21:02
Hamid_RT25-Sep-07 21:02 
AnswerRe: Can we post data on web site through VC++ application? Pin
megha_gharote25-Sep-07 21:04
megha_gharote25-Sep-07 21:04 
GeneralRe: Can we post data on web site through VC++ application? Pin
Hamid_RT25-Sep-07 21:25
Hamid_RT25-Sep-07 21:25 
AnswerRe: Can we post data on web site through VC++ application? Pin
Naveen25-Sep-07 21:18
Naveen25-Sep-07 21:18 
QuestionNeed msgrua.h and msgruaid.h files Pin
Rahul Vaishnav25-Sep-07 20:34
Rahul Vaishnav25-Sep-07 20:34 
AnswerRe: Need msgrua.h and msgruaid.h files Pin
toxcct25-Sep-07 21:05
toxcct25-Sep-07 21:05 
GeneralRe: Need msgrua.h and msgruaid.h files Pin
Rahul Vaishnav26-Sep-07 0:00
Rahul Vaishnav26-Sep-07 0:00 
QuestionUninstaller Pin
yadahav25-Sep-07 19:39
yadahav25-Sep-07 19:39 
AnswerRe: Uninstaller Pin
Hamid_RT25-Sep-07 20:25
Hamid_RT25-Sep-07 20:25 
QuestionActivex mfc - urgent pls Pin
deeps_cute25-Sep-07 18:37
deeps_cute25-Sep-07 18:37 
AnswerRe: Activex mfc - urgent pls Pin
User 58385225-Sep-07 20:04
User 58385225-Sep-07 20:04 
GeneralRe: Activex mfc - urgent pls Pin
jhwurmbach26-Sep-07 1:54
jhwurmbach26-Sep-07 1:54 
QuestionMFC42.DLL Pin
rishimohan25-Sep-07 18:19
rishimohan25-Sep-07 18:19 
AnswerRe: MFC42.DLL Pin
Hamid_RT25-Sep-07 19:43
Hamid_RT25-Sep-07 19:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.