Hello Everyone!!!
Welcome Back!!! At the end of this module, you will be able to control and specify decimal points in float or double data types in C.
Welcome Back!!! At the end of this module, you will be able to control and specify decimal points in float or double data types in C.
Intro
Many times, we use float data type to represent any result like CGPA marks, statistical data, average or any other data. While representing this data with float, the result will be shown in 6 decimal digits like 1.000000. We are going to code that can specify the decimal digits as our needs and requirements like 1.00 result.
Let's See the Before and After Effects
Before:
I have created one float variable "a" which holds the value of 1. In the next line, I have printed "a".
Here, it prints useless 6 decimal digits. While we want to print mathematical data like the average of marks, it will be very awkward to print 6 decimal digits. So, it can be avoided by the trick given below.
After:
Trick: Insert the dot and number of decimal digits you want in output between "%" and "f"
Ex. In the above picture, I have inserted a dot and number of decimal digits I wanted in output i.e. 2.
Output:
It will print the float value with only 2 decimal points.
Here We Go !!!
Outro
I have used Nano Text Editor to build and run the code. If you want to learn about Nano Text Editor, Please checkout <Tutorial 1>, <Tutorial 2>, <Tutorial 3>.
If you like it, please share it with your friends & leave your valuable opinions below.
Comments
Post a Comment