There was once this little function
string format_dollars_for_screen(float d)
{
char ch[50];
sprintf(ch,"%1.2f",d);
return ch;
}
who liked to return -0.00
.
I modified it to
string format_dollars_for_screen(float d)
{
char ch[50];
float value;
sprintf(ch,"%1.2f",d);
value = stof(ch);
if(value==0.0f) sprintf(ch,"0.00");
return ch;
}
And it started returning 0.00
as desired. My question is, why doesn't this other solution work?
string format_dollars_for_screen(float d)
{
char ch[50];
float value;
sprintf(ch,"%1.2f",d);
value = stof(ch);
if(value==0.0f) sprintf(ch,"%1.2f", value);
return ch;
}
And/or is there a more efficient way to do this? This is just off the top of my head, so critiques are welcome. =)