Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printf how to do floating points with leading zeros

Tags:

java

c

php

printf

perl

I know how to do X amount of leading zeros, and I know how to do X amount of decimal points. But, how do I do them both?

I am looking to have 4 leading zeros to a decimal precision of 2: 0000.00. Therefore 43.4 would be 0043.40

like image 865
ParoX Avatar asked Jul 09 '10 11:07

ParoX


People also ask

How do you add leading zeros in printf?

You can do zero padding before the Decimal point. fprintf(stdout, " PRINT - INTEGER : %05d and FLOAT : %014.8f n", num_int, num_flt); It will show total 14 digits and 8 digits after decimal point.

How do you add leading zeros to a number in C?

Step 1: Get the Number N and number of leading zeros P. Step 2: Convert the number to string using the ToString() method and to pad the string use the formatted string argument “0000” for P= 4. val = N. ToString("0000");

How do I print floating point numbers?

You can do it like this: printf("%. 6f", myFloat); 6 represents the number of digits after the decimal separator.


1 Answers

Try this printf (C, Perl, PHP) format string:

"%07.2f"
like image 96
Marcelo Cantos Avatar answered Oct 30 '22 02:10

Marcelo Cantos