string FormatNumber(double dbltext, string textformat)
{
string text=to_string(dbltext);
string outputtext="";
int intFormat=0;
int inttext=0;
for(intFormat=0,inttext=0; intFormat<textformat.size() or inttext<text.size(); intFormat++,inttext++)
{
if(textformat[intFormat]=='#')
outputtext+=text[inttext];
else
{
outputtext+=textformat[intFormat];
inttext=inttext-1;
}
}
//getting the count of numbers and zeros for right and left
int intdecimal=0;
int intnodecimal=0;
bool blndecimaldot=false;
for(int i=0; i<textformat.size(); i++)
{
if(textformat[i]=='#' and blndecimaldot==false)
intnodecimal+=1;
elseif(textformat[i]=='.' or textformat[i]==',')
blndecimaldot=true;
elseif (textformat[i]=='#' and blndecimaldot==true)
intdecimal+=1;
}
//give the zeros on tight and left
char *chrtext;
string format=(string)"%0"+to_string(intnodecimal)+ "." + to_string(intdecimal)+"f";
MessageBox(NULL,format.c_str(),"testing", MB_OK);
double mycharp =atof(outputtext.c_str());
constchar *myformatp =format.c_str();
MessageBox(NULL,to_string(mycharp).c_str(),"testing", MB_OK);
//chrtext2=outputtext.c_str();
sprintf(chrtext,myformatp,mycharp);
//getting the final result for return it
outputtext=chrtext;
return outputtext;
}
but i get a memory leak.
the sprintf() isn't recive, correctly, the values :(
can anyone advice me?