Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop large float numbers from being output in exponential form. (scientific notation?)

Tags:

c++

I have a program that calculates population growth. It seems to be working but after when the population exceed 1 million it is output as a decimal number raised to a power of ten. (is this called scientic notation? exponential form? i forget.)

Is there anyway to output the data as a full number? Here is the code for the 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;
    } 
like image 477
darko Avatar asked Dec 29 '22 04:12

darko


2 Answers

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

like image 57
icecrime Avatar answered Feb 05 '23 05:02

icecrime


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

like image 41
pejotr Avatar answered Feb 05 '23 04:02

pejotr