I wrote a small program that reads two integers using scanf , and then performs various arithmetic calculations. I use printf to display the results. How can I make printf display only two digits after the decimal point? Starting with a simplified code example:
#include <stdio.h> int main(void) { double third = 1.0 / 3.0; // display data printf("\n%20s%20s", "Description", "Data"); printf("\n%20s%20s", "-----------", "----"); printf("\n%20s%20lf", "One third", third); printf("\n"); return 0; }
Prints "0.333333" for the value of third . How can I change above to get the following result?
Description Data
----------- ----
One third 0.33
c decimal
Sarah dawkins
source share