zkvsl
zkvsl

Reputation: 89

Printing a decimal place

How do I get 1.371429 as 01.37. Is it not %2.02f? I need to have a leading zero and also round it to two decimal places.

Upvotes: 4

Views: 120

Answers (1)

Mukit09
Mukit09

Reputation: 3409

Try this:

#include<stdio.h>

int main()
{
    double a = 1.2345666;
    printf("%05.2lf\n", a);
    return 0;
}

Here, 05 says "print 5 columns with leading zeros". .2 says "print decimal digits untill 2 columns". 2 decimal digits + 1 . + 2 integer part => total 5 columns you have to print.

Upvotes: 1

Related Questions