How to stop large float numbers from exponentially output. (scientific notation?)

I have a program that calculates population growth. It seems to work, but after the population exceeds 1 million, it is displayed as a decimal number increased to ten. (This is called scientific notation? exponential form? I forget.)

In any case, to output the data as a complete number? Here is the code to output where I will have to convert it.

#include "header.h" void output (float currentPopulation, float years, float birthRate, float deathRate) { cout << "the populaion in " << years << " years will be: " << estimatedPopulation (currentPopulation, years, birthRate, deathRate) << endl; } 

New code:

 #include "header.h" void output (float currentPopulation, float years, float birthRate, float deathRate) { cout << "the populaion in " << years << " years will be: " << fixed << setprecision(0) << estimatedPopulation (currentPopulation, years, birthRate, deathRate) << endl; } 
+4
source share
5 answers

std::fixed manipulator used in conjunction with std::setprecision should help you (remember #include <iomanip> ).

+7
source

With float, you get only 24 bits of accuracy, which is about 16,777,216. If you are dealing with numbers that exceed this and you need higher precision, consider using double , although I think you still have to do some formatting, to make it look the way you want. In this case, I recommend that you look at http://www.arachnoid.com/cpptutor/student3.html and http://www.fredosaurus.com/notes-cpp/io/omanipulators.html .

 cout << fixed << estimatedPopulation (currentPopulation, years, birthRate, deathRate) 
+1
source

I suggest reading about std::scientific : http://msdn.microsoft.com/en-us/library/szb722da(VS.71).aspx

+1
source
 float v(1234567891000000.0f); cout.precision(0); cout << fixed << v << endl; 

Note the loss of precision inherent in float: output is

1234567948140544

+1
source

Use printf instead, for example printf("%f", myvalue) .

0
source

All Articles