-3

What is the equivalent of .ToString("00") in C++?

I'm getting an error stating that

left of '.ToString' must have class/struct/union
1>        type is 'double'

Update: Thanks for the replies, i'm having another similar issue now //inorder to get //.ToString("00.00000") i did the following

  memset(buf, 0, sizeof(buf));
  sprintf_s(buf, "%02.7f",latm); //.7 to match heremisphere
  std::string latm_str = buf;

i realised that the %02 doesnt have any effects, example when i get a 7.0, the result is 7.0000000 rather than the desired 07.0000000, anything wrong here?

user1157977
  • 907
  • 1
  • 11
  • 17
  • 5
    "What is English for 'Je suis un rock star'?" is a pretty useless question unless you know that the original phrase is in French :-) What source language is your original in? – paxdiablo Oct 16 '14 at 08:23
  • sprintf(string_buffer, "%02d", value) – 9dan Oct 16 '14 at 08:23
  • 3
    What language is `ToString("00");` in? Also `"00"` already looks like a string. – Galik Oct 16 '14 at 08:24
  • 1
    I thinks its C# and you use it like this: `double a = 3.14; string s = a.ToString("00"); // will be 03` – Tomashu Oct 16 '14 at 08:30
  • From the error and its text, I'd guess you are doing `someDouble.ToString( "00" )`. I don't know where you got this from, but it certainly isn't C++. In C++, conversions to text are done using `std::ostringstream` (sometimes wrapped in function). – James Kanze Oct 16 '14 at 08:31

4 Answers4

3

I think you should use std::to_string

RJFalconer
  • 10,890
  • 5
  • 51
  • 66
Etixpp
  • 320
  • 2
  • 11
1

I'm guessing that this is C# based on this link. If so depending on whether you have access to C++11, you can probably use the following:

int a = 3;
std::string s = std::to_string(a);

If you're not yet using C++11 then you can use the following:

int a = 3;
std::ostringstream ss;
ss.fill('0');
ss << std::setw(2) << a;
std::string s = ss.str();

More detail is on this question

Community
  • 1
  • 1
Component 10
  • 10,247
  • 7
  • 47
  • 64
1
double number = 3.14;
char buf[100];
sprintf_s(buf, "%02d", (int)number);
string s = buf;
cout << s; // prints 03

Based on Custom string formatting: ToString("00")

Community
  • 1
  • 1
Tomashu
  • 515
  • 1
  • 11
  • 20
  • thanks for your reply, just wondering what if its .ToString("00.00000") ? Do i just need to do a sprintf_s(buf, "%2.5f", number); – user1157977 Oct 16 '14 at 08:50
0

it's std::to_string in C++, what you are looking for.

double abc = 23.233;
std::string mystring = std::to_string(abc);
Zeeshan
  • 2,884
  • 3
  • 28
  • 47