-2

Let's say I have:

f = ((100+x)/x)*1.5. 

where f is of type float.

How can I convert it to a character string with a specified precision?

metasim
  • 4,793
  • 3
  • 46
  • 70

4 Answers4

1

How can I convert it to a character string with a specified precision?

There are 2 ways to consider precision for a double/float x:

In an relative sense

int precision = 7;
// print to the 1+7 most significant digits
printf("%.*e\n", precision, x);

In an absolute sense

int precision = 7;
// print to the closest 0.0000001
printf("%.*f\n", precision, x);

Both have the issue of string/space management when printing to a string. Assuming a buffer that will not overflow, it is simple enough to use sprintf() instead of printf().

int precision = 7;
//     sign dig  .   precision   e  sign expo \0
char buf[1 + 1 + 1 + precision + 1 + 1 + 5 + 1];
// print to the 1+7 most significant digits
sprintf(buf, "%.*e", precision, x);

Using %f is tricky as the number could be DBL_MAX needing hundreds or thousands of digits.

int precision = 7;
//     sign  ------digs--------   .   precision  \0
char buf[1 + DBL_MAX_10_EXP + 1 + 1 + precision + 1];
// print to the closest 0.0000001
sprintf(buf, "%.*f", precision, x);

A classic approach is to use snprintf() to compute the space needed by calling it once with 0 space and then determining the size needs. snprintf() will not overfill the given size of the buffer. Still, snprintf() results need to be checked to detect cases when the buffer was undersized.

int need = snprintf(NULL, 0, some_format, x);
assert(need >= 0);
char *buf = malloc(need + 1u);
assert(buf);
int need2 = snprintf(buf, need + 1u, same_format_as_before, x);
assert(need2 == need);
...
// do something with buf
...
free(buf);

Note: It is easy to mistakenly undersize a buffer's size considering extreme numbers and locale effects.

Note: Some code here assumes the exponent will not exceed 5 digits or 99999. A robust solution would right size as needed.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • I am sorry. I should have include my use case. I am gonna send this float to Uart port. I tried several ways with sprintf, but it's not worked. I have to use the built in function: appWriteDatatoUsart(). which in my case decimal part is not showing correctly. but integer part is good. whenever I use sprintf. it shows '?' mark. By the way thanks for your response. – Ariful Islam Jun 07 '16 at 06:34
  • Without seeing your code, perhaps `char buf[40]; sprintf("%.*e\n", DBL_DECIMAL_DIG - 1, x);` [ref](http://stackoverflow.com/a/19897395/2410359). Post your code as maybe a new question (if it differs from this post much) might be best. – chux - Reinstate Monica Jun 07 '16 at 13:20
0

Always try to use Standard Library functions. Creating string from float, with precision, you can use sprintf.

Example:

#include <stdio.h>
#include <math.h>

int main()
{
   char str[80];
   sprintf(str, "Value of Pi = %f", M_PI);

   return(0);
}
beremaran
  • 47
  • 12
0

Use the snprintf to encode a float as a string and place it into a character buffer you provide. The second parameter is the size of the buffer to ensure that you don't overflow the space available. You also need to check the result of snprintf to ensure the string was created successfully without overflow.

#include <assert.h>
#include <stdio.h>

int main()
{
      float number_as_float = 6.02;
      char number_as_ascii[40];
      assert(snprintf(number_as_ascii, 40, "%f", number_as_float) <= 40u);
      return(0);
}
caskey
  • 12,305
  • 2
  • 26
  • 27
-1

Use the sprintf function.

float f = 100/3;
char *s = malloc(128);
sprintf(s, "%f", f);
user24100
  • 27
  • 1
  • 6
  • Wrong way. `atof` converts a string to a float. The result would not be a string. – dawg Jun 03 '16 at 23:22
  • 1
    Now you have no allocation now for memory that `*s` would point to. Please delete or correct! – dawg Jun 03 '16 at 23:27
  • `128` is generous but insufficient on many systems with `sprintf(s, "%f", DBL_MAX);` Perhaps [`1 + DBL_MAX_10_EXP + 1 + 1 + 6 + 1`](http://stackoverflow.com/a/37625313/2410359)? – chux - Reinstate Monica Jun 04 '16 at 01:30