954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Convert a float to a string - need a simple helper function

I have a basic knowledge of C++ but mostly in a limited fashion. I am building an API and I need to send the resulting string to a text box in a Modal Dialog. I will not need to manipulate the string beyond sending it to the text box.

The float can possibly get pretty long but not beyond 8 decimal places or so.

So any short and sweet helper function would be of great help.

Morbane
Newbie Poster
12 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Here's a simple way to make the conversion using snprintf().

#include <iostream>
#include <cstdio>

int main()
{
  char ans[20];
  float myf = 123.0 + (1.0 / 3.0);
  
  snprintf(ans, 20, "%f", myf);
  
  std::cout << "ans->" << ans << std::endl;
  return 0;
}
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

This is a nice conversion function at will convert any type that has an << operator.

#include <string>
#include <sstream>

template<typename T>
string ConvertToString(T & value)
{
    std::stringstream ss;
    ss << value;
    return ss.str();
}
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

Ok those both look real simple, but I really need a return value of type string so I can call the function and set it equal to a variable to use in the API Dialog code.

I should have specified but I am still learning.

std::string ftostr(float myfloat)
{
  char ans[20];
  //float myf = 123.0 + (1.0 / 3.0);

  snprintf(ans, 20, "%f", myfloat);

  //std::cout << "ans->" << ans << std::endl;
  return ans;
}

I tried that but my Code::Blocks compiler does not recognise snprintf. I know I am just fumbling around but at least I'm trying.

Morbane
Newbie Poster
12 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Ok those both look real simple, but I really need a return value of type string so I can call the function and set it equal to a variable to use in the API Dialog code.

I should have specified but I am still learning.

std::string ftostr(float myfloat)
{
  char ans[20];
  //float myf = 123.0 + (1.0 / 3.0);

  snprintf(ans, 20, "%f", myfloat);

  //std::cout << "ans->" << ans << std::endl;
  return ans;
}

I tried that but my Code::Blocks compiler does not recognise snprintf. I know I am just fumbling around but at least I'm trying.

Did you include the cstdio library? If you want to return a string then cast ans as a string.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

Ok cstdio worked. I have the function looking like this:

char ftostr(float myfloat)
{
    char ans[20];

    snprintf(ans, 20, "%f", myfloat);

    return *ans;
}

But when I call the function like so:

SetDlgItemText(hwndDlgM, IDC_CONVERTION, sNum);

The third parameter sNum needs to be a const char. Any way around that?

Morbane
Newbie Poster
12 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Oops.

I call the function like this:

char sNum = ftostr(fNum);

Then the SetDlgItemText() attempts to use the converted char.

Sorry 'bout that.

Morbane
Newbie Poster
12 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

OK - I came up with this:

char *ftostr(float myfloat)
{
    char ans[20];

    snprintf(ans, 20, "%f", myfloat);

    return ans;
}

And I call it like this:

char *sNum = ftostr(fNum);

SetDlgItemText(hwndDlgM, IDC_CONVERTION, sNum);

It all compiles but I ran into another problem. That is for another thread though.

Morbane
Newbie Poster
12 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: