9

I have an array of floats where data are stored with varying decimal points so some are 123.40000, 123.45000, 123.45600...now if i want to print these values in the string without the 0s in the end in printf() so that they are 123.4, 123.45, 123.456, without those 0s in the end. Is this possible? If so, how?

4 Answers 4

19

Use the %g formatter:

printf( "%g", 123.4000 );

prints

123.4

Trailing zeros are removed, but unfortunately so is the trailing decimal point if the fractional part is zero. I don't know if there is actually any way of doing what you want directly using printf() - I think something like this is probably your best bet:

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

void print( FILE * f, double d ) {
    if ( d - floor(d) == 0.0 ) {
        fprintf( f, "%g.", d );
    }
    else {
        fprintf( f, "%g", d );
    }
}

int main() {
    print( stdout, 12.0 );
    print( stdout, 12.300 );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't it a bad idea to compare doubles? "if (d-floor(d) == 0.0)"?
@himself No, it's fine when you know what you are doing. “Never compare floating-point numbers for ==” is just an approximation for the unsophisticated.
3

I don't know how hacky this is but:

http://codepad.org/e3Q3pUNd

float f = 124.000;
if (f == (int) f) {
    printf("%.1f\n", f); /* .1 can be changed */
} else {
    printf("%g\n", f);
}

Returns 124.0.

float f = 124.123000;
if (f == (int) f) {
    printf("%.1f\n", f); /* .1 can be changed */
} else {
    printf("%g\n", f);
}

Returns 124.123.

Comments

0

Use %g --

Print a double in either normal or exponential notation, whichever is more appropriate for its magnitude. 'g' uses lower-case letters, 'G' uses upper-case letters. This type differs slightly from fixed-point notation in that insignificant zeroes to the right of the decimal point are not included. Also, the decimal point is not included on whole numbers.

1 Comment

just a small query, what if i want to remove the trailing zeros but want to keep the decimal in whole numbers, like 124.00000 is 123.
0

Print to a (large enough) buffer. Print the buffer ... and if the there's no '.' in the buffer print a dot.

char buf[100];
sprintf(buf, "%g", val);
printf("%s", buf);
if (strchr(buf, '.') == NULL) putchar('.');

edit

The Standard specifies the # flag:

# The result is converted to an ``alternative form''. [...] For a, A, e, E, f, F, g, and G conversions, the result of converting a floating-point number always contains a decimal-point character, even if no digits follow it. [...] For g and G conversions, trailing zeros are not removed from the result. [...]

... but you get the trailing zeros :(

Comments

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.