views:

266

answers:

7

i.e. can printf be told to ignore zero when it precedes the decimal point?

+1  A: 

In a word: No...

DevSolar
printf() cant,but we can!
fahad
Yes `printf` can. See my answer.
R..
@R..: No it cannot. See my comment to your answer.
DevSolar
A: 

See the printf spec, and the precision section. You can do printf("%+0.4f", -0.5);

Dead account
sorry didn't work
sharkin
Well? Downvote mouse-over text reads: "This answer is not useful". This does apply since your answer does not solve the OP's question. (Not *my* downvote, but still - no reason to get personal.)
DevSolar
-1 for being a complete jerk in the comment; it's uncalled for an entirely inappropriate here.
James McNellis
Ok - Im going to delete my account.
Dead account
+1  A: 

Sure it can, but I'm afraid you won't be too happy with my answer:

printf("-.5", -0.5);
ammoQ
That will produce a warning, as there is no format spec in the "-.5" string :). I appreciate a joke, but the answer should probably be a Community Wiki.
Alexander Pogrebnyak
Alexander: agreed
ammoQ
A: 

I would try something like:

char *_rm0_(double d, char *_buff){ 
    sprintf(_buff, "%f", d);
    if( (int)d==0 ){
        _buff++;
        if(d<0) _buff[0]='-';
    }
    return _buff;
}

int main() {
    double my_double1=0.5, my_double2=-0.5;
    char _buff[10][10];
    printf(" value: %s 2nd val: %s\n", _rm0_(my_double1, _buff[0]), _rm0_(my_double2, _buff[1]) );
    return 0;
}
perreal
A: 
R..
Wrong. `%g` is specified as either `%f` or `%e` depending on the value to be printed, and both of those are specified to have a digit in front of the decimal point. A quick test confirms this: `printf( "%g", -0.5 );` yields "-0.5" on my machine (Linux).
DevSolar
My bad. I was reading it as a request to remove the trailing zeros, not the leading zero.
R..
The new edits should provide a simple and correct approach.
R..
all those strikeouts make my eyes bleed.
mrduclaw
A: 

Convert your double or float to a char then:

while (*yourChar != '\0') {
    if (yourDoubleBeforeConversion > 1.0 && yourDoubleBeforeConversion < -1.0) {
        continue;
    }
    if(*yourChar == '.') {
        yourChar--;
        *yourChar = '';
    }
    else {
    }
}

Explination:

  1. while loop determines when the char ends.
  2. If the double you had is greater than 1 then there is no point in trimming.
  3. If it isn't keep checking for a ".".
  4. When one is found go before and delete the zero.
thyrgle
+3  A: 

With all the things that are specified in ISO C99, this is not possible. The documentation for the conversion specifiers says:

f,F: [...] If a decimal-point character appears, at least one digit appears before it. [...]

e,E: [...] there is one digit (which is nonzero if the argument is nonzero) before the decimal-point character [...]

g,G: [...] is converted to style f or e [...]

Since the other conversion specifiers would not print a decimal floating-point number, this is impossible at all. Unless you find a vendor extension that introduces another conversion specifier. But then your program would not be strictly valid C anymore.

The best workaround that comes to my mind is:

double number = 0.5;
char buf[80];
char *number = buf;
if (snprintf(buf, sizeof buf, "%f", number) <= 0)
  goto cannot_happen;

while (*number == '0')
  number++;
printf("%s\n", number);
Roland Illig