How to print floating point numbers with a specified precision? Rounding is not required. For example, 5.48958123 should be printed as 5.4895 if given precision is 4.
For example, below program sets the precision for 4 digits after the decimal point:
We can generalize above method using pow()
In C, there is a format specifier in C. To print 4 digits after dot, we can use 0.4f in printf(). Below is program to demonstrate the same.
This article is contributed by Niharika Khandelwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- What are the C programming concepts used as Data Structures
- size of char datatype and char array in C
- Argument Coercion in C/C++
- Is there any equivalent to typedef of C/C++ in Java ?
- Working of Keyword long in C programming
- C/C++ program to find the size of int, float, double and char
- Uninitialized primitive data types in C/C++
- int (1 sign bit + 31 data bits) keyword in C
- Program to find out the data type of user input
- restrict keyword in C
- Compound Literals in C
- sizeof() for Floating Constant in C
- C | Data Types | Question 9
- How to print range of basic data types without any library function and constant in C?