2

I have a data type of double, I want to printf the double in the following form:

  • If double x=2 or x=2.0 print 2 to the screen, which means if I get an integer I print an integer.
  • If double x=2.3 or x=2.30 or any other non-whole number I print 2.3 to the screen.

Is there any way to do it shortly? or do I need to use if statements or something like that?

double x = 2;
printf("%d", x);
double x = 2.3;
printf("%.1f", x);

This is an illustration only. I want to printf an integer or a double based on the value I get.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
operout
  • 37
  • 3
  • 3
    Maybe investigate the `%g` format specifier? – Adrian Mole May 17 '20 at 17:26
  • 2
    `%d` is always wrong to print a `double` value, doesn't matter if it holds an integral value or not. – RobertS supports Monica Cellio May 17 '20 at 17:36
  • Otherwise, that might not be very easy. Suppose, due to the inexactness of floating point values, `2.3` is stored as `2.29999999`? How will you know how many decimals to print? Please see [Why Are Floating Point Numbers Inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Weather Vane May 17 '20 at 17:45
  • operout, If the `double` had the value of `2.29999999999999982236431605997495353221893310546875`, what output would you want to see? "2.29999999999999982236431605997495353221893310546875" or "2.3"? This may be useful [Function to print a double - exactly](https://codereview.stackexchange.com/q/212490/29485) – chux - Reinstate Monica May 18 '20 at 00:55

3 Answers3

0

The quick and dirty way to print a double with a minimal number of decimals without an extra loop is to use the %g conversion specifier.

#include <stdio.h>

void print_num(double x) {
     printf("%g", x);
}

Very small and very large values may require a more explicit approach to avoid the exponent form:

#include <stdio.h>

void print_num(double x) {
    char buf[500];
    int n = snprintf(buf, sizeof buf, "%.15f", x);
    while (n > 0 && buf[n - 1] == '0') {
        buf[--n] = '\0';
    }
    if (n > 0 && buf[n - 1] == '.') {
        buf[--n] = '\0';
    }
    printf("%s", buf);
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

If OP wants one decimal place after the . when the value of not a whole number:

Use ".*" and trunc() to control number of precision digits.

trunc() returns the double with the fractional part truncated.

double f = 2.3;
printf("%.*f\n", f != trunc(f), f);  // 1 digit
f = 2.0;
printf("%.*f\n", f != trunc(f), f);   // 0 digits, no .

Output

2.3
2
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

Consider i is an int variable. Assign value of x into the i before check like i==x. It will be assigned truncate value of x. then

if(i==x) printf("%d",i);

i==x will be only true when x won't have any fragment.

else printf("%.1f",x);
  • 1
    This fails when the floating-point value to be converted overflows `int`. There are truncation functions for floating-point types, so there is no need to involve integer types. Also, conversion to integer does not take the floor; it truncates. – Eric Postpischil May 17 '20 at 23:34
  • @EricPostpischil Thank you very much for your kind information. – Md Asaduzzaman May 19 '20 at 01:16