Convert a double to a string

This is driving me daft!

Page 1 of 1

4 Replies - 23037 Views - Last Post: 26 February 2008 - 10:24 AM Rate Topic: -----

#1 TheMagnitude  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 144
  • Joined: 12-January 08

Convert a double to a string

Posted 25 February 2008 - 03:47 PM

How do I convert a double to a string? I've searched the web and tried this:

double rep=13.87;
char buffer[16];
sprintf(buffer,"%d",rep);

but it doesnt seem to work :(

Can anyone help?
Is This A Good Question/Topic? 0
  • +

Replies To: Convert a double to a string

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Convert a double to a string

Posted 25 February 2008 - 04:00 PM

Double's format specifier is %f , %d is used for int.

Try that, if it doesn't work please post the contents of buffer.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Convert a double to a string

Posted 25 February 2008 - 04:18 PM

You can use sprintf() as you have done to convert a double to a string, but I would actually recommend using _snprintf() instead simply because sprintf() has no regard for the fact that strings are fixed length devices in memory and will overflow if you don't watch it. _snprintf() allows you to specify the length of the out string, just be sure to specify the size as one less than the actual allocated memory block, because _snprintf() does not store the terminating null character if it has to cut the output short.

An example us using _snprintf() is:

void ToString(char * outStr, int length, double val)
{
    _snprintf(outStr,length,"%f",val);
}



Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#4 TheMagnitude  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 144
  • Joined: 12-January 08

Re: Convert a double to a string

Posted 26 February 2008 - 09:58 AM

View PostPsychoCoder, on 25 Feb, 2008 - 04:18 PM, said:

You can use sprintf() as you have done to convert a double to a string, but I would actually recommend using _snprintf() instead simply because sprintf() has no regard for the fact that strings are fixed length devices in memory and will overflow if you don't watch it. _snprintf() allows you to specify the length of the out string, just be sure to specify the size as one less than the actual allocated memory block, because _snprintf() does not store the terminating null character if it has to cut the output short.

An example us using _snprintf() is:

void ToString(char * outStr, int length, double val)
{
    _snprintf(outStr,length,"%f",val);
}



Hope this helps :)
Thank you this method works perfectly =P
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Convert a double to a string

Posted 26 February 2008 - 10:24 AM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1