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.

5 Answers 5

3

what happened to the good old %f

Sign up to request clarification or add additional context in comments.

4 Comments

Does not get desired output. His question should of asked more directly.
@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.
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.
@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 :)
1
"%f"

example

printf("%f\n", floatVal);

2 Comments

I need fixed size output string.
nor will "%f" show the + sign in front of positive numbers.
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.

1 Comment

This requires calculating needed number of decimal places manually in code. what is the way to do this with code for any case?
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

2 Comments

I think snprintf(str, 8, "%#+.5f", 0.0003); best of all
@user1020803 That does work in all cases. Got lost in all my changes not sure why i was still using sig figs after i switched to snprintf(). Thanks. edited to reflect.
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.

1 Comment

I thought about it, but hoped for the best solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.