5

I have a question about using printf.

char str[8];
float val = 2.334563;
sprintf(str, format, val);
printf("val = %s.\n", str);

val = -23.34563;
sprintf(str, format, val);
printf("val = %s.\n", str);

val = -0.02334563;
sprintf(str, format, val);
printf("val = %s.\n", str);

val = 233;
sprintf(str, format, val);
printf("val = %s.\n", str);

The expected output follows:

val = +2.3345
val = -23.345
val = -0.0233
val = +233.00

What format string do I need for that? Thank you for your attention.

Wez Sie Tato
  • 1,186
  • 12
  • 33
user1020803
  • 59
  • 1
  • 3

5 Answers5

3

what happened to the good old %f

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Does not get desired output. His question should of asked more directly. – Joe McGrath Oct 30 '11 at 16:54
  • @JoeMcGrath: This Q clearly smells of Homework & I do not appreciate writing any form of code for Homework Questions other than giving pointers or ideas. **Reason:** I do not want an programmer sitting right beside me(Albeit a few yrs from now) who cannot write a source code given a hint. – Alok Save Oct 30 '11 at 16:59
  • Ok thanks. I am new to Stack Overflow and did not realize. Just trying to answer questions. I am a hobbyist programmer who hasn't been to school. Did not think of homework assignments. Will remember to do this in future. – Joe McGrath Oct 30 '11 at 17:05
  • 2
    @JoeMcGrath: Yes,please.I understand your enthusiasm and willingness to help but We do not really want to turn SO in to a **Do My Homework** Website.Usually,for Q's which smell of homework We would like to see what the OP has tried,their efforts and their ideas to solve the problem.Then provide subtle hints/clues which will give them a direction and unblock them but also leaves them enough sizeable homework to do.As a hobbyist programmer you would sure know,there is no alternative to hard work.Welcome to SO and See you around.All the Best :) – Alok Save Oct 30 '11 at 17:14
1
"%f"

example

printf("%f\n", floatVal);
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
1

use snprintf() to truncate string at exactly 8 characters including \0

Format string:

"%#+.5f"

%   modifier
#   force decimal place
+   show + or -
.5  precision of 5
f   use precision as places after decimal 

Code:

char str[8];
float val = 2.334563;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

val = -23.34563;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

val = -0.02334563;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

val = 233.001;
snprintf(str,8, "%#+.5f", val);
printf("val = %s\n", str);

Output:

val = +2.3345
val = -23.345
val = -0.0233
val = +233.00
val = +0.0003
Joe McGrath
  • 1,481
  • 10
  • 26
1

The following (almost) does what you want. Note that I changed the number of characters in the str array from 7 to 8; since all of your output strings contain 7 characters the NULL termination performed by sprintf will cause buffer overflow otherwise.

The only difference between my results and yours is the rounding performed by sprintf. AFAIK, the only way to get around this is to pre-truncate the number you want to print using floor; for example, to print 2 digits without rounding float f = floor( 1.8888 * 100 ) / 100;

#include <stdio.h>

int main(void)
{
  char str[8];

  {
    float val = 2.334563f;
    sprintf(str, "%+6.*f", 4, val);
    printf("val = %s.\n", str);
  }

  {
    float val = -23.34563f;
    sprintf(str, "%+6.*f", 3, val);
    printf("val = %s.\n", str);
  }

  {
    float val = -0.02334563f;
    sprintf(str, "%+6.*f", 4, val);
    printf("val = %s.\n", str);
  }

  {
    float val = 233.0f;
    sprintf(str, "%+6.*f", 2, val);
    printf("val = %s.\n", str);
  }

  return 0;
}

Output:

val = +2.3346.
val = -23.346.
val = -0.0233.
val = +233.00.
Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • This requires calculating needed number of decimal places manually in code. what is the way to do this with code for any case? – Joe McGrath Oct 30 '11 at 16:30
0

The only thing that I can come up with is as follows:

const char *format = "val = %+6.*f\n";
int places_past_decimal = ???;
printf(format, places_past_decimal, 2.334563f);

In this case, you will need to pass an additional argument (places_past_decimal) which is the number of digits remaining in the number that aren't used by the left side of the number.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173