Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using _ecvt or _fcvt???

Status
Not open for further replies.

jayjay60

Programmer
Jun 19, 2001
97
FR
In my application i want to return a value into an edit box. i know that i have to change the value in a string type value.
So, as the value i want to return is a double i would like to use the function _ecvt.
But i have a pb with it, because if i want to return for example a number as 3.14, there is no pb, but if the value is 0.0314, the first 0 after "." is not taken.
In my code i do that:
int decimal,sign;
.
.
.
m_dlgPrice=0.0314;
strParam=_fcvt(m_dlgPrice,4,&decimal,&sign);
CString strInt=strParam.Left(decimal);
CString strDecimal =strParam.Mid(decimal); strParamFin.Format("%s.%s",strInt,strDecimal);
CEdit* Edit=(CEdit*)GetDlgItem(IDC_PRICE);
Edit->SetWindowText(strParamFin);

and in the edit box i could only see .314, so where is the pb?

thanks in advance for your help.

jayjay
 
For the value 0.0314, _fcvt gives you 314 and decimal has the value -1, meaning the decimal point ist to the left of the string. You need to prefix abs(decimal)+1 zeros if decimal is negative. You need something like the following code, although there's maybe a simpler solution and it needs a bit of sanity checking...

double m_dlgPrice=0.0314;
int decimal,sign;
CString zeros("000000");
CString strParam = _fcvt(m_dlgPrice,4,&decimal,&sign);
if (decimal<1)
{
decimal = abs(decimal);
strParam = zeros.Left(decimal+1) + strParam;
decimal = 1;
}
CString strInt=strParam.Left(decimal);
CString strDecimal = strParam.Mid(decimal);

CString strParamFin;
strParamFin.Format(&quot;%s.%s&quot;,strInt,strDecimal); :) Hope that this helped! ;-)
 
Hi guys,
Is there any reason why not to do it this way?
Code:
double d = 0.0314;
CString str;
str.Format( &quot;%.4f&quot;, d );
 
i try hcexi proposition, and it works.
thanks for your help

jayjay
 
I thought there was a simpler way - moment of blindness:) :) Hope that this helped! ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top