lundi 28 novembre 2016

Set the digits in a float after decimal point

I want to write a c++ function which converts a float to const char *. Here in this function, an argument is passed to specify the decimal places that output value should represent. I have come up this functions and it works nicely.

I would like to whether, there are any better ways write this function?.

static const char* getString(float value, int decimalPlaces)
{
     char strValue[sizeof value];
     sprintf(strValue, "%.%df", value, decimalPlaces);
     return strValue;
}

Aucun commentaire:

Enregistrer un commentaire