Since you did not specify any constraints for the implementation, the simplest way to do it would be:
std::string to_string(float f) {
std::ostringstream buffer;
buffer << f;
return buffer.str();
}
And the same then for double
and long double
.
It would also be valid to have an array of char
large enough and use sprintf
.
EDIT:
As stated in the comments, this is to be an implementation in the arm. One algorithm I quickly implemented to do this is as follows:
std::string to_string(double value, unsigned precision=2) {
const bool negative = value < 0;
if (negative) {
value *= -1;
}
//Move a casa decimal para a esquerda o quanto for necessário
for (unsigned i=0; i<precision; ++i) {
value *= 10;
}
//Converte para inteiro e calcula quantos dígitos vai ter
unsigned long long i_value = value;
int digits_count = 0;
for (unsigned long long v=i_value; v!=0; v /= 10) {
++digits_count;
}
std::string result;
result.resize(digits_count+1); //+1 para o '.';
//Converte para string e põe o '.' no lugar necessário
int prec_counter = precision;
for (int pos = result.size()-1; pos >= 0; --pos) {
if (prec_counter == 0) {
result[pos] = '.';
}
else {
int digit = i_value % 10;
i_value /= 10;
result[pos] = '0' + digit;
}
--prec_counter;
}
//Remove zeros à direita depois do . decimal
if (precision > 0) {
while (result.back() == '0') {
result.pop_back();
}
if (result.back() == '.') {
result.pop_back();
}
}
return (negative ? "-" : "") + result;
}
This is a simple and limited algorithm, but it can serve as a basis for something more elaborate. I have not tested much, and it does not treat special cases like NAN
. I'm assuming your std::string
has the functions I've used.