Float to String

Y

YLD

Hi,

How could I copy a float to a string?
For example:

float number = 12.34;
char numberstring[5];
*introduce some magic code here*
resulting in numberstring == [ '1' | '2' | '.' | '3' | '4' | '\0' ]

Or soemthing similar... I thought it would be something very easy, but
after looking the FAQ and searching google I cant find an appropriate
way.

Thank you.
 
E

Emmanuel Delahaye

YLD wrote on 10/09/05 :
How could I copy a float to a string?

Do you meant 'convert' ?
float number = 12.34;
char numberstring[5];
*introduce some magic code here*
resulting in numberstring == [ '1' | '2' | '.' | '3' | '4' | '\0' ]

sprintf() is your friend. Be sure than the destination array of char is
big enough.


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
Y

YLD

Thank you very much Emmanuel, the sprintf() hint was all that I needed
and of great help.

However, after searching about that I found out that asprintf() is much
safer, as you don't have to deal with how much space the destination
array needs. It is also very simple.
I post a sample code just in case anybody gets here looking for the
same information:

#include <stdio.h>

int main()
{
char *my_string;

asprintf (&my_string, "Being %d is cool, but being free is best of
all.", 4);
puts (my_string);

return 0;
}

Bye!
 
I

Irrwahn Grausewitz

YLD said:
Hi,

How could I copy a float to a string?
<snip>

This is a faq, see section 13.1 (faq link in signature).

Short version: use sprintf
I thought it would be something very easy, [...]

It is. :)

However, I suggest you read section 12.21 of the faq about avoiding
buffer overruns when using sprintf.
 
E

Emmanuel Delahaye

YLD wrote on 10/09/05 :
However, after searching about that I found out that asprintf() is much
safer, as you don't have to deal with how much space the destination
array needs. It is also very simple.

Sounds good, unfortunately, it's not standard. However, a portable
implementation of this should be possible to achieve.

Note also that C99 supports snprintf().

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
I

Irrwahn Grausewitz

YLD said:
Thank you very much Emmanuel, the sprintf() hint was all that I needed
and of great help.

However, after searching about that I found out that asprintf() is much
safer, as you don't have to deal with how much space the destination
array needs. It is also very simple.

Alas, asprintf is not standard C. If you happen to have a conforming
C99 implementation at hand, there's snprintf.

Regards
 
E

Emmanuel Delahaye

YLD wrote on 10/09/05 :
Thank you very much Emmanuel, the sprintf() hint was all that I needed
and of great help.

However, after searching about that I found out that asprintf() is much
safer, as you don't have to deal with how much space the destination
array needs. It is also very simple.
I post a sample code just in case anybody gets here looking for the
same information:

#include <stdio.h>

int main()
{
char *my_string;

asprintf (&my_string, "Being %d is cool, but being free is best of
all.", 4);
puts (my_string);

return 0;
}

Bye!

Of course, you want a test and a memory release:

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#if defined (linux)
#define F_NIL "/dev/null"
#elif defined (MSDOS) || defined (WIN32)
#define F_NIL "nul"
#else
#error not defined for this platform
#endif

int asprintf (char **pp_out, char const *fmt,...)
{
int n;
FILE *fp = fopen (F_NIL, "wb");

if (fp != NULL)
{
va_list va;
va_start (va, fmt);
{
n = vfprintf (fp, fmt, va);
fclose (fp);

{
char *s_out = malloc (n + 1);

if (s_out != NULL)
{
int ns = vsprintf (s_out, fmt, va);

assert (n == ns);

if (pp_out != NULL)
{
*pp_out = s_out;
}
}
else
{
n = -1;
}
}
}
va_end (va);
}
else
{
n = -1;
}
return n;
}


int main (void)
{
char *my_string;

asprintf (&my_string, "Being %d is cool, but being free is best of
all.", 4);

if (my_string != NULL)
{
puts (my_string);
free (my_string), my_string = NULL;
}

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

YLD wrote on 10/09/05 :
Thank you very much Emmanuel, the sprintf() hint was all that I needed
and of great help.

However, after searching about that I found out that asprintf() is much
safer, as you don't have to deal with how much space the destination
array needs. It is also very simple.
I post a sample code just in case anybody gets here looking for the
same information:

#include <stdio.h>

int main()
{
char *my_string;

asprintf (&my_string, "Being %d is cool, but being free is best of
all.", 4);
puts (my_string);

return 0;
}

Bye!

Of course, you want a test and a memory release:

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#if defined (linux)
#define F_NIL "/dev/null"
#elif defined (MSDOS) || defined (WIN32)
#define F_NIL "nul"
#else
#error not defined for this platform
#endif

int asprintf (char **pp_out, char const *fmt,...)
{
int n;
FILE *fp = fopen (F_NIL, "wb");

if (fp != NULL)
{
va_list va;
va_start (va, fmt);
{
n = vfprintf (fp, fmt, va);
fclose (fp);

{
char *s_out = malloc (n + 1);

if (s_out != NULL)
{
int ns = vsprintf (s_out, fmt, va);

assert (n == ns);

if (pp_out != NULL)
{
*pp_out = s_out;
}
}
else
{
n = -1;
}
}
}
va_end (va);
}
else
{
n = -1;
}
return n;
}

int main (void)
{
char *my_string;

int n = asprintf (&my_string, "Being %d is cool, but being free is
best of all.", 4);

if (n > 0 && my_string != NULL)
{
puts (my_string);
free (my_string), my_string = NULL;
}

return 0;
}


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
B

Barry Schwarz

Thank you very much Emmanuel, the sprintf() hint was all that I needed
and of great help.

However, after searching about that I found out that asprintf() is much
safer, as you don't have to deal with how much space the destination
array needs. It is also very simple.

But is asprintf standard?


<<Remove the del for email>>
 
Y

YLD

This is a faq, see section 13.1 (faq link in signature).
Short version: use sprintf

I did look in the FAQ, but I guess I looked at the wrong section.
I didn't know asprintf wasn't standard.

Probably I will use sprintf and use a long array. Any way, if the
array is not long enough, what would be the worse that could happen?
Will I just miss precision? (I just need the first 4 significant
figures..)

Thanks again
 
E

Emmanuel Delahaye

YLD wrote on 10/09/05 :
I didn't know asprintf wasn't standard.

Google "man asprintf" "I'm lucky"

http://www.hmug.org/man/3/asprintf.php

HISTORY

The functions asprintf() and vasprintf() first appeared in the GNU
C
library. These were implemented by Peter Wemm <[email protected]>
in
FreeBSD 2.2, but were later replaced with a different
implementation from
Todd C. Miller said:
Probably I will use sprintf and use a long array. Any way, if the
array is not long enough, what would be the worse that could happen?

Anything (Undefined behaviour). It's a serious bug.
Will I just miss precision? (I just need the first 4 significant
figures..)

'figures '? You meant 'digits', I guess...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
I

Irrwahn Grausewitz

YLD said:
I did look in the FAQ, but I guess I looked at the wrong section.
I didn't know asprintf wasn't standard.

Probably I will use sprintf and use a long array. Any way, if the
array is not long enough, what would be the worse that could happen?

Since you might invoke undefined behaviour by writing to memory you
don't own, absolutely anything can happen. Make sure the array is
large enough.
Will I just miss precision? (I just need the first 4 significant
figures..)

Specify the precision in the printf format string, e.g.:

sprintf( your_buf, "%.4g", your_double );

This will give you 4 significant digits, plus decimal point, plus
optional sign, plus eventually some characters for the exponent, plus
the terminating '\0'. It's difficult to tell beforehand, how many
characters the exponent will take, but at least 4 - two digits, sign
and 'e'. However, a generously oversized buffer will do. I hope I
won't get fried for making such a fuzzy assertion in c.l.c. :eek:)

Anyway, there's always one more option: write the number to a file
first, and the return value of fprintf will tell you the number of
characters written. Clumsy, but safe.

If you go for the %f format: IIRC there is a rule of thumb to estimate
the number of characters in the textual representation of a floating
point number in "ordinary" decimal notation. Alas, I'm unable to
reproduce it right now, I'm somewhat unorganized at the moment. I
wonder if anyone else could help me out?

Best Regards
 
O

Old Wolf

YLD said:
Hi,

How could I copy a float to a string?
For example:

float number = 12.34;
char numberstring[5];
*introduce some magic code here*
resulting in numberstring == [ '1' | '2' | '.' | '3' | '4' | '\0' ]

You can't fit 6 characters into an array of 5 chars.
My advice is for you to use snprintf() -- it's supported by
most C implementations, and if you find one where it isn't,
then it is easy to download some free source code for the function.

You mentioned you were going to use asprintf -- it seems to me that
any 'asprintf' implementation will need to refer to 'vsnprintf' anyway,
in order to work out how much memory to allocate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top