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
- Measure execution time with high precision in C/C++
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Rounding Floating Point Number To two Decimal Places in C and C++
- C/C++ For loop with Examples
- Arrow operator -> in C/C++ with Examples
- return statement in C/C++ with Examples
- size of char datatype and char array in C
- Logical Not ! operator in C with Examples
- Difference between C and C#
- scanf("%[^\n]s", str) Vs gets(str) in C with Examples
- time.h localtime() function in C with Examples
- Conditional or Ternary Operator (?:) in C/C++
- C program to Insert an element in an Array
- Types of Literals in C/C++ with Examples