Formatting day in future


#1

Hi all together,

first of all I must fully agree, that the book is really great! Thanks for that!!

When I tried to solve the challenge I was struggling with the task itself. So when I finally understood what to do I found a solution.

What I didn’t like so far was the date and also the time format like 3-4-2012 12:2:3. I’d prefer 03-04-2012 12:02:03. Then I played around with the formats we learned so far.
Surprisingly the following line of code did the job for me:

printf("The shifted time will be %.2d.%.2d.%d %.2d:%.2d:%.2d\n", shiftedTime.tm_mday, shiftedTime.tm_mon+1, shiftedTime.tm_year + 1900, shiftedTime.tm_hour, shiftedTime.tm_min, shiftedTime.tm_sec); 

So using %.2d also added the leading zero. So far I only understood that it formats decimal numbers always fix with two decimals. Could someone explain me why it also adds the leading zeros??

Regards
MrKrapps


#2

Using a period followed by an integer in a format string for numbers indicates that the precision of the displayed number is equal to the integer in the string.

So, %.1f indicates that the float should be rounded to 1 digit after the decimal point, so 3.235 would be displayed as 3.2.

When you’re formatting integers instead of floats, the period followed by an integer indicates that the integer should be represented with that number of digits. Since it’s a whole number, the only way to pad the number (while generating a purely numeric value, so no spaces are used) is to precede it with 0s.

So %.5d means display a whole number at least 5 digits long, padding the front with 0 if needed to achieve that width. printf("%.5d", 15) would display 00015.


#3

Thanks macintux for the explanation.
That’s really helpful.

Regards
MrKrapps


#4

If I’m not mistaken it doesn’t round, it truncates. So 3.235 would be 3.2 as you stated, but 3.275 would still turn out as 3.2.
Please do correct me though if I’m mistaken, I’m still fairly new to this.


#5

It rounds:

Johns-MacBook-Air:tmp jdaily$ cat foo.c
#include <stdio.h>

int main(void) {
printf("%.1f\n", 3.399);
return 0;
}
Johns-MacBook-Air:tmp jdaily$ gcc foo.c
Johns-MacBook-Air:tmp jdaily$ ./a.out
3.4


#6

[quote]%.1f indicates that the float should be rounded to 1 digit after the decimal point[/quote]Got that.

[quote]When you’re formatting integers instead of floats, the period followed by an integer indicates that the integer should be represented with that number of digits. [/quote]Wouldn’t be the first time, but where did I miss reading that in the book? :blush: BTW, if it is in there, I will simply blame my aging eyes! :open_mouth: Although I do remember seeing/learning about that when floats were covered. :unamused:


#7

I doubt this level of detail was covered in the book. I had to go digging through the man page to refresh my memory before replying.


#8

But… but… I want everything in one book, preferably with large print and all the details without the need to search elsewhere and possibly without actually having to read each paragraph! LOL! It would be even more helpful if I could simply leave the book beside my bed and have it be absorbed while I sleep. :stuck_out_tongue:

Thanks, again, for the extra details that simply can’t be included in this very good book. I’m sure that even after finishing it, I’ll still be a wee bit short of being hired to supervise a group of programmers! :open_mouth: