|
I am having trouble converting double to char. The number can be 15 digits in length with
maximum of 5 decimal precision or minimum 0. Tried using sprintf/ltoa but it gives some
wierd results.
sprintf when used in this format sprintf(buffer,"%.5f",dNum); works fine if there are 10
digits to the left of the decimal. For digits >10 to the left of decimal i.e 11,12,13 the results are incorrect.
Code Snippet:
char buffer[20];
CString csTest;
dNum = 99999999999.999;
sprintf(buffer,"%.5f",dNum);
csTest.Format(_T("%.5f"),dNum);
Test cases with 11 digits to the left
INPUT (dNUM) OUTPUT
99999999999.99999 9999999999.99999
99999999999.9999 99999999999.99989
99999999999.999 99999999999.99899
99999999999.99 99999999999.99001
99999999999.9 99999999999.89999
I tried using the CStrings' Format function but I get the same result as that function also inherently uses sprintf.
|
|
|
|
|
|
That sounds so zen.
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
Save an Orange - Use the VCF!
|
|
|
|
|
I take it that you actually want to pad the the number with trailing 0's.
Here's 1 method:
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
char buffer[64];
char Format[32];
double dNum = 99999999999.0;
int MaxPrecision = 5;
int Precision;
double Divisor = 10.0;
for(Precision = 0; Precision <= 5; Precision++)
{
sprintf(Format, "%%.%uf", Precision);
int CharCount = sprintf(buffer, Format, dNum);
if(CharCount >= 0)
{
int i;
if(!strchr(buffer, '.'))
{
buffer[CharCount] = '.';
CharCount++;
}
for(i = 0; i < MaxPrecision - Precision; i++)
buffer[CharCount + i] = '0';
buffer[CharCount + i] = 0;
}
printf("%s\n", buffer);
dNum = dNum + (9 / Divisor);
Divisor *= 10;
}
getch();
return 0;
}
|
|
|
|
|
Sorry, I misinterpreted your question in my previous reply.
The short answer is that you have exceeded the floating point (or is it printf?) library precision by using 11 + 5 digits (excluding the decimal point).
This example may help you understand what is going on:
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <conio.h> // for getch()
int _tmain(int argc, _TCHAR* argv[])
{
char buffer[64];
double dNum;
int MaxPrecision = 8;
int Precision;
double Divisor;
long MaxDecimal;
for(double dblNum = 8888; dblNum <= 88888888888; dblNum = dblNum * 10 + 8)
{
for(int CurPrecision = 1; CurPrecision <= MaxPrecision; CurPrecision++)
{
dNum = dblNum;
Divisor = 10.0;
fprintf(stdout, "\nCurrent Precision: %u\n\n", CurPrecision);
for(Precision = 0, MaxDecimal = 0; Precision < CurPrecision; Precision++)
MaxDecimal = MaxDecimal * 10 + 9;
for(Precision = 0; Precision <= CurPrecision; Precision++)
{
int CharCount;
CharCount = sprintf(buffer, "%.*f", CurPrecision, dNum);
fprintf(stdout, "%s\t", buffer);
CharCount = sprintf(buffer, "%.*f", Precision, dNum);
if(CharCount >= 0)
{
int i;
if(!strchr(buffer, '.'))
{
buffer[CharCount] = '.';
CharCount++;
}
for(i = 0; i < CurPrecision - Precision; i++)
buffer[CharCount + i] = '0';
buffer[CharCount + i] = 0;
}
fprintf(stdout, "%s\n", buffer);
dNum = dNum + (9 / Divisor);
Divisor *= 10;
}
}
}
getch();
return 0;
}
If you run the example, you will notice that things become decidedly 'iffy' when you reach the 16 char output limit. The reason the second sprintf statement works better is that it is forcing rounding of the decimal part (so it must be printf that is the culprit), but since your problem states that the maximum number of digits is 15, you should have no problem with these limits.
|
|
|
|
|
Try something like this. I use code very similar to this all the time. NOTE I have not tested the negative double (the insertion of the - sign may need to be adjusted.
void convertMyDoubleToString(double myDouble,int precision,CString& result) const{
char* buffer;
int dec;
int sign;
size_t i;
buffer = _fcvt(myDouble,precision,&dec,&sign);
for (i=strlen(buffer)-1;i>(size_t)sign;i--){
if (i == dec){
buffer[i+1] = buffer[i];
buffer[i] = '.';
break;
}else{
buffer[i+1] = buffer[i];
}
}
result = buffer;
if (myDouble >= 0 && myDouble < 1){
result = CString("0.") + result.Left(result.GetLength()-1);
}else if (myDouble < 0 && res > 1){
result = CString("-0.") + result; //MAY NEED TO ADJUST THIS
}else if (myDouble <= 1){
result = CString("-") + result; //MAY NEED TO ADJUST THIS
}
}
|
|
|
|
|
Try
sprintf(buffer,"%.5lf",dNum);
(remark the l between 5 and f)
SkyWalker
|
|
|
|
|
Is there a way I can find out if a given path is on a drive on the user's PC vs. a remote drive on a LAN. The path could be a UNC, a mapped drive, a local drive or no drive spec. The UNC could still be the local machine.
Suggestions?
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
Is PathIsNetworkPath() close? There are other Pathxxx() functions, too.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Thanks David, I didn't think to look at the Shell functions. I'll give this a try.
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
I realize its not the stl forum but theres no traffic in there:
I want to get the index of the min element and am doing:
std::vector<int>::iterator start,end, location ;
start = m_distances.begin();
end = m_distances.end();
location = min_element(start,end);
qDebug("%d min val %d %d",*location, *start, *end);
int index = location - start;
but it doesnt work. I always get 181 as the index.
??
Thanks,
sb
|
|
|
|
|
Hi,
has anyone done the above before in C++? I need to do it and don't know where to start...
Thanks,
Pia
|
|
|
|
|
Start by having a look at GDI+. You'll find articles here and at MS.
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
There are several articles at http://www.codeproject.com/bitmap/[^] that cover the topic. The most light weight ones being the ones that wrap the IPicture interface.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it! Honoured as one of The Most Helpful Members of 2004
|
|
|
|
|
Hey all
My question is how to find a child window!
well, I know that I can use FindWindow("hwndclass", NULL); but how to find a child window of a program? I tried using FindWindow() but it couldn't recognize the child window... is there any way?
thanks in advance
|
|
|
|
|
EnumChildWindows()?
Artificial intelligence is no match for natural
stupidity.
|
|
|
|
|
|
After Using the FindWindow() ,For enumerating Child Window use FindWindowEx() .
if you need,I will Provide you code for Enumerting the Desktop Windows.
"I Think this Will Help"
<h5
alok gupta="" <br=""> visit me at http://www.thisisalok.tk
|
|
|
|
|
FindWindowEx allows you to specify a parent handle, and will search only for children of that parent window.
So you find the parent window, then call FindWindowEx using the parent window handle as the starting search point.
There is one VERY important caveat:
FindWindowEx will ONLY find FIRST generation children.
Suppose for example, your top level window is a dialog box. The Dialog has a frame control. The window you want to find is a child of the frame.
Calling FindWindowEx using the parent will find the frame. But it will not find your desired window, because it only searches direct children.
So your strategy in this case would be:
1) Get dialog handle.
2) Use dialog handle as parent and find handle to the frame.
3) Then use the frame handle, NOT the dialog handle, as the new parent and continue the search.
This will now find your target because it is a direct child of the frame, not the dialog.
Because windows hierarchies can be complex, you need to use a tool like Spy which can show the relationships of a window. Then base you search strategy based on how Spy shows you the windows are related.
Hope this helps,
Robert
|
|
|
|
|
Thanks alot for your replaies....
but does this also work with a popup window?
I mean, many programs have option window... let's take Yahoo for example, if I click on option from the menu then a popup window will come and disable the main yahoo list, so can I also use FindWindowEx to find this popup window?
thanks alot
|
|
|
|
|
Well, usually the use of FindWindowEx implies that you are finding and possibly manipulating windows in a program external to your own program, because you should already inherently know how to find all windows your own program creates.
So, assuming you are doing what I think you are doing, the answer is yes, because the popup is modal to the browser window that spawned it, but it is not modal to your own program. So yes, you can still execute code and should be able to find it. Again, use Spy to help you design the strategy to find the popup.
I have a program that creates a browser window and then navigates to a site page that displays a popup modal dialog. I have no problems finding the login dialog, and entering the login automatically.
So I think the answer to your question is yes, unless you are doing something really weird.
Robert
|
|
|
|
|
well...
when I try using spy++ then I found that the class of the windows is #32770 (Dialog) and the window caption is option, and when I use FindWindow() and use SendMessage() to close it, nothing happen!
HWND optionwindow =::FindWindow(NULL, "option");
or
HWND optionwindow =::FindWindow("#32770", NULL);
or even
HWND optionwindow =::FindWindow("#32770", "option");
and
::SendMessage(optionwindow, WM_CLOSE, 0, 0);
but not working!
any help thanks?
|
|
|
|
|
I'm writing again since nobody has answered my previous post about the strange error I get under win98/ME and 2000 (debug assertion failure at daocore.cpp @ line 453, but the code actually works perfect under winxp sp2.
CDaoDatabase db;<br />
CDirectory dir;<br />
CString strDb = dir.GetCurDir() + _T("db.udb");<br />
<br />
if(GetFileAttributes(strDb) == 0xFFFFFFFF)<br />
{<br />
try{<br />
db.Create(strDb);<br />
CString strSQL;<br />
strSQL = _T("CREATE TABLE table (id COUNTER);");<br />
db.Execute(strSQL);<br />
db.Close();<br />
} catch (CDaoException *e)<br />
{<br />
e->ReportError();<br />
e->Delete();<br />
}<br />
}
Please help
|
|
|
|
|
I forgot to mention that it fails on the target machine (client), so may be it's something with redistribution or what?
|
|
|
|
|
|