CString to double with _stscanf_s_l

Viewed 135

I am in a c ++ (2010), MFC, COleVariant context.

I am facing a problem converting between a CString and a double using _stscanf_s_l.

Indeed some string is found converted with a large number of decimal places without my understanding why.

I present an example to illustrate the case:

CString szChaine(newValue.bstrVal); 
// --> szChaine =
// --> Example 1 : "102.09"
// --> Example 2 : "102.10" 
// --> Example 3 : "102.11" 
// --> Example 4 : "14.44"
// --> Example 5 : "14.45"
// --> Example 6 : "14.46"


double dbl;

_locale_t loc = _create_locale(LC_ALL, "C");
int nRet = _stscanf_s_l(szChaine,_T("%lf"), loc ,&dbl);
_free_locale(loc);

//--> dbl = 
// --> Example 1 : 102.09
// --> Example 2 : 102.09999999999999
// --> Example 3 : 102.11 
// --> Example 4 : 14.44
// --> Example 5 : 14.449999999999999
// --> Example 6 : 14.46

With or without a locale the problem remains the same.

newValue is a COleVariant of type VT_BSTR. My goal is to get a double. I tried the ChangeType method on the COleVariant like this newValue.ChangeType(VT_R8);

but ChangeType throws an exception, hence my attempt with _stscanf_s_l.

Do you have an explanation or a solution?

0 Answers
Related