105
double x = 1500;
for(int k = 0; k<10 ; k++){
    double t = 0;
    for(int i=0; i<12; i++){
        t += x * 0.0675;
        x += x * 0.0675;
    }
    cout<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;      
}

this the output

Bas ana: 3284.78 Son faiz: 1784.78 Son ana: 5069.55

Bas ana: 7193.17 Son faiz: 3908.4 Son ana: 11101.6

Bas ana: 15752 Son faiz: 8558.8 Son ana: 24310.8

Bas ana: 34494.5 Son faiz: 18742.5 Son ana: 53237

Bas ana: 75537.8 Son faiz: 41043.3 Son ana: 116581

Bas ana: 165417 Son faiz: 89878.7 Son ana: 255295

Bas ana: 362238 Son faiz: 196821 Son ana: 559059

Bas ana: 793246 Son faiz: 431009 Son ana: 1.22426e+006

Bas ana: 1.73709e+006 Son faiz: 943845 Son ana: 2.68094e+006

Bas ana: 3.80397e+006 Son faiz: 2.06688e+006 Son ana: 5.87085e+006

I want numbers to be shown with exact numbers not scientific numbers. How can I do this?

Community
  • 1
  • 1
Yunus Eren Güzel
  • 3,018
  • 11
  • 36
  • 63
  • 9
    Why are you casting a `double` to `double`? – Fred Foo Mar 06 '11 at 17:18
  • 2
    possible duplicate of [Prevent scientific notation in ostream when using << with double](http://stackoverflow.com/questions/2335657/prevent-scientific-notation-in-ostream-when-using-with-double) – Mechanical snail Aug 22 '12 at 03:38

9 Answers9

136

Use std::fixed stream manipulator:

cout<<fixed<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;
Tugrul Ates
  • 9,451
  • 2
  • 33
  • 59
42

As mentioned above, you can use std::fixed to solve your problem, like this:

cout << fixed;
cout << "Bas ana: " << x << "\tSon faiz: " << t << "\tSon ana: " << x+t <<endl;

However, after you've done this, every time you print a float or a double anywhere in your project, the number will still be printed in this fixed notation. You could turn it back by using

cout << scientific;

but this might become tedious if your code gets more complicated.

This is why Boost made the I/O Stream State Saver, which automatically returns the I/O stream you're using to the state it was before your function call. You can use it like this:

#include <boost/io/ios_state.hpp> // you need to download these headers first

{
    boost::io::ios_flags_saver  ifs( os );

    cout << ios::fixed;
    cout<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;

} // at this bracket, when ifs goes "out of scope", your stream is reset

You can find more info about Boost's I/O Stream State Saver in the official docs.

You may also want to check out the Boost Format library which can also make your outputting easier, especially if you have to deal with internationalisation. However, it won't help you for this particular problem.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Migi
  • 1,112
  • 9
  • 14
16

code the next syntax:

std::cout << std::fixed << std::setprecision(n);

where (n) is the number of decimal precision. This should fix it.

P.s.: you need to #include <iomanip> in order to use std::setprecision.

Dinei
  • 4,494
  • 4
  • 36
  • 60
Ahmad Yones
  • 175
  • 3
  • 5
8

In C++20 you'll be able to use std::format to do this:

std::cout << std::format("Bas ana: {:f}\tSon faiz: {:f}\t"
                         "Son ana: {:f}\n", x, t, x + t);

Output:

Bas ana: 3284.776791    Son faiz: 1784.776791   Son ana: 5069.553581
Bas ana: 7193.172376    Son faiz: 3908.395585   Son ana: 11101.567961
...

The advantage of this approach is that it doesn't change the stream state.

In the meantime you can use the {fmt} library, std::format is based on. {fmt} also provides the print function that makes this even easier and more efficient (godbolt):

fmt::print("Bas ana: {:f}\tSon faiz: {:f}\tSon ana: {:f}\n", x, t, x + t);

Disclaimer: I'm the author of {fmt} and C++20 std::format.

vitaut
  • 49,672
  • 25
  • 199
  • 336
3

You can use format flags

More info: http://www.cplusplus.com/reference/iostream/ios_base/fmtflags/

muhoweb
  • 116
  • 3
2

You can use the function fixed in this way :

std::cout << std::fixed << ...

Or you can use the the printf function with a standard format like "12.12%f" in this way :

printf ("number = %12.12f", float_number);

Zig Razor
  • 3,381
  • 2
  • 15
  • 35
1

There are a whole collection of formatting operators that you get with iostream. Here's a tutorial to get you started.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
0

You can also use printf in c++ to print in the value without scientific notation.

As You can also usecin and cout instead of scanf and printf, however, if you are taking a million numbers as input and printing a million lines, it is faster to use scanf and printf.

#include<stdio.h>
#include<math.h>

  int main () { 
  double a = pow(2,99); 
  printf ("%lf\n",a); 
  return 0; 
  }

Output

633825300114114700748351602688.000000
Apurv Jha
  • 454
  • 3
  • 17
0

Use this code:

double x = 1500;
  for(int k = 0; k<10 ; k++){
    double t = 0;
    for(int i=0; i<12; i++){
        t += x * 0.0675;
        x += x * 0.0675;
    }
    cout<<"Bas ana: "<< fixed << x <<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;      
}
  • You can use << fixed << to show exact numbers and not scientific numbers.

  • It is a small, easy fix ;)

***If you want the ouput to be in 2 decimal places, you can use this code: ***

(Use #include <iomanip> and then type << setprecison(n) << " after " << fixed << , where 'n' is the number of decimal places you want. So, in your case, you can use 2 for n and get 2 decimal places. The full code is written below.)

#include<iostream>
#include<iomanip>

using namespace std;

int main() {
  double x = 1500;
  for(int k = 0; k<10 ; k++){
    double t = 0;
    for(int i=0; i<12; i++){
        t += x * 0.0675;
        x += x * 0.0675;
    }
    cout<<"Bas ana: " << fixed << setprecision(2) << x <<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<< endl;      
}
}
avariant
  • 2,234
  • 5
  • 25
  • 33