Header Ads Widget

How to fix limit in double and floating numbers after dot (.) in c++

 Here in this article we will learn how to fix the no. of numbers in the double and floating point numbers (i.e 235.2356 after dot how many numbers there will be ) . For this we can use precision() . In this method we can write cout.precision(x) where value of x determine how many number there will me after dot (.) .

Method 1:-

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    double a=123.4589;
    double b=1.0e6;
    double c=1256.32;
    
    printf("%.2lf  %.2lf %.2lf",a,b,c);
    return 0;
}
output:-

123.46 1000000.00 1256.32


Method 2:-

  In this method we will use precision function  which is predefine in iostream library file .
by using this we can simply set a limit a after dot in the floating and double numbers.
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    double a=123.4589;
    double b=1.0e6;
    double c=1256.32;
    
    cout.precision(2);
    
    // In fixed notation
    cout<<"In fixed notation"<<endl;
    cout<<fixed<<a<<" "<<b<<" "<<c<<endl;
    
    // In scientific notation
    cout<<"In Scientific Notation"<<endl;
    cout<<scientific<<a<<" "<<b<<" "<<c<<endl;
    return 0;
}
Output:-

In fixed notation
123.46 1000000.00 1256.32
In Scientific Notation
1.23e+02 1.00e+06 1.26e+03


what is fixed and Scientific notation:-

What is fixed and scientific notation (Exponential notation)




Recommended Post:

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

Key points:-

 MCQs:-