Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide leading zero in printf

Tags:

c

double

printf

The following outputs 0.23. How do I get it to simply output .23?

printf( "%8.2f" , .23 ); 
like image 571
Christian Mann Avatar asked Apr 07 '10 20:04

Christian Mann


People also ask

What does %% mean in printf?

As % has special meaning in printf type functions, to print the literal %, you type %% to prevent it from being interpreted as starting a conversion fmt.

How do you remove floating trailing zeros?

To remove the trailing zeros from a number, pass the number to the parseFloat() function. The parseFloat function parses the provided value, returning a floating point number, which automatically removes any trailing zeros.

What does omit the leading zero mean?

When leading zeros occupy the most significant digits of an integer, they could be left blank or omitted for the same numeric value. Therefore, the usual decimal notation of integers does not use leading zeros except for the zero itself, which would be denoted as an empty string otherwise.

What is leading zeros in C?

If the position of zero before bit is set to one, they are termed as leading zeros.


1 Answers

The C standard says that for the f and F floating point format specifiers:

If a decimal-point character appears, at least one digit appears before it.

I think that if you don't want a zero to appear before the decimal point, you'll probably have to do something like use snprintf() to format the number into a string, and remove the 0 if the formatted string starts with "0." (and similarly for "-0."). Then pass that formatted string to our real output. Or something like that.

like image 60
Michael Burr Avatar answered Oct 07 '22 02:10

Michael Burr