2

How can you convert the string 6.78 to a float? How can you convert a float 7.89 to a string?

Do both of these questions have similar answers?

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
user1082764
  • 1,973
  • 9
  • 26
  • 40

3 Answers3

3

In the standard library <string> you have

std::string std::to_string(float val)

and

float std::stof (const std::string&  str, size_t* idx = 0)

See stof and to_string

A.E. Drew
  • 2,097
  • 1
  • 16
  • 24
0

I agree with the AE Drew it works fine You can also use boost lexical cast if you have boost installed. Here is an example I found.

http://code-better.com/c/convert-string-integer-using-boost-lexicalcast

I also found that this question has been asked already so you can enjoy a fully contributed answer here:

How to convert a number to string and vice versa in C++

Hope it helps

Community
  • 1
  • 1
Gabriel
  • 3,564
  • 1
  • 27
  • 49
0

What is your environment?

If it is Visual Studio and Windows, use atoi() and itoa().

user1343318
  • 2,093
  • 6
  • 32
  • 59