Home c++ Convert number to string

Convert number to string

Author

Date

Category

How can I convert a number to text using the C++ language and output it to the console?
For example:

float x = 0.05f;
char * str;
// str = x

Answer 1, authority 100%

Option 1.

# include & lt; stdio.h & gt;
printf ("Number is% f \ n", x);

Option 2.

# include & lt; iostream & gt;
std :: cout & lt; & lt; "Number is" & lt; & lt; std :: scientific & lt; & lt; x & lt; & lt; std :: endl;

Answer 2, authority 92%

Here are C methods like atoi, sprintf, etc. for converting to C++. can be fraught with consequences. Here is a purely C++ version based on streams, and a universal one at that. You can convert even integer types, even floating point.

`

# include & lt; string & gt;
#include & lt; sstream & gt;
template & lt; typename T & gt;
std :: string toString (T val)
{
  std :: ostringstream oss;
  oss & lt; & lt; val;
  return oss.str ();
}
template & lt; typename T & gt;
T fromString (const std :: string & amp; s)
{
 std :: istringstream iss (s);
 T res;
 iss & gt; & gt; res;
 return res;
}

Used as follows

std :: string str;
int iVal;
float fVal;
str = toString (iVal);
str = tiString (fVal);
iVal = fromString & lt; int & gt; (str);
fVal = fromString & lt; float & gt; (str);

I took an example from here


Answer 3, authority 96%

solution in C++ 11
http://www.cplusplus.com/reference/string/to_string/

# include & lt; string & gt;
int x = 10;
std :: string y = std :: to_string (x);

Answer 4, authority 43%

Use the sprintf function from stdio.h:

float x = 0.05f;
char str [20];
sprintf (str, "% f", x); // or any other format like printf ()
std :: cout & lt; & lt; str & lt; & lt; std :: endl;

Answer 5, authority 29%

strstream s;
float x = 0.05f;
s & lt; & lt; x; // convert to string

Answer 6, authority 14%

In C++ 20 it will be possible to use the function std :: format , which returns the finished string.

Until compiler support is implemented, reference can be made via fmt :: , eg :

# include & lt; fmt / core.h & gt;
#include & lt; iostream & gt;
#include & lt; string & gt;
int main () {
  float x = 0.05f;
  std :: string s = fmt :: format ("{}", x);
  std :: cout & lt; & lt; s & lt; & lt; '\ n';
}

The s variable is completely optional and added for clarity and TS desires to have the result exactly in the line.


Answer 7

# include "stdafx.h"
#include & lt; iostream & gt;
#include & lt; sstream & gt;
using namespace std;
int _tmain (int argc, _TCHAR * argv [])
{
  float a = 33.5;
  int b = 50;
  char str [50];
  _TOSTRING (str, "% f", a);
  cout & lt; & lt; str & lt; & lt; endl;
  _TOSTRING (str, "% d", b);
  cout & lt; & lt; b;
  return 0;
}

Answer 8

string flts (long x)
{
  string s;
  char c;
  while (x)
  {
    c = (x% 10) + '0';
    s = c + s;
    x = x / 10;
  }
  return s;
}

Answer 9

I have implemented a function to convert a number of any type to a string in this way.

value: the number to be converted

radix: number system (2/8/10/16)

dec_places: number of decimal places (for a real number)

* if mant_len is not specified, then all signs will be converted, and this is not always necessary, since due to the peculiarities of the representation of floating point numbers, the result will not be what we expect.

For example:

ntos (-0.12) will return: -0.119999999999999996447286321199499070644378662109375

But if you explicitly specify the required number of decimal places, for example 2:

ntos (-0.12, 10, 2) will return: -0.12

/ *********************** ********************************************** /
/ * Function of converting the number of any type to the string * /
/ * ================================================ =================== * /
/ * value: Number to convert * /
/ * radix: Numeral system - 2 (bin) / 8 (oct) / 10 (dec) / 16 (hex) * /
/ * dec_places: Number of the places to the right of the decimal point * /
/ *********************************************** ********************* /
template & lt; typename T & gt; string ntos (T value, uint8_t radix = 10, int32_t dec_places = -1)
{
static const char ns [] = "0123456789ABCDEF";
  string str = "\ 0";
  switch (radix) {
    case 2:
    case 8:
    case 10:
    case 16: break;
    default: return str;
  }
  uint8_t mod, index = 0;
  / * Handler for the signed part of the nuber * /
  if (value & lt; 0) {/ * It is a negative number * /
    value = abs (value);
    if (value & amp; & amp; dec_places! = 0) {
      index = 1;
      str.insert (0, "-");
    } else {
      / * Value less than 0 and decimal places value is 0 * /
      str.insert (0, "0");
      return str;
    }
  }
  / * Handler for the unsigned part of the number * /
  uint64_t integer = uint64_t (value);
  do {/ * Convert an integer part to string * /
    mod = integer% radix;
    integer / = radix;
    str.insert (index, (const char *) & amp; ns [mod], 1);
  } while (integer);
  / * Handler for the places to the right of the decimal point of the number * /
  value - = (uint64_t) value; / * Substract an integer part * /
  if (value & amp; & amp; dec_places! = 0) {
    uint64_t len ​​= 0;
    if (dec_places & gt; 0) {
      / * Rounding * /
      uint32_t pow10 = 1;
      for (uint32_t i = 0; i & lt; dec_places; i ++)
        pow10 * = 10;
      value * = pow10;
      value + = 0.5f;
      value / = pow10;
    }
    str + = '.';
    do {
      value * = 10;
      integer = (uint64_t) value;
      value - = integer;
      mod = integer% radix;
      integer / = radix;
      str + = (const char) ns [mod];
    } while (value & amp; & amp; ++ len! = dec_places);
  }
  return str;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions