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,304
  • 4
  • 59
  • 73
user1082764
  • 1,883
  • 8
  • 24
  • 38

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,028
  • 1
  • 15
  • 23
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,486
  • 1
  • 25
  • 48
0

What is your environment?

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

user1343318
  • 2,033
  • 3
  • 31
  • 54