[noob][c++] cout + float + precision

I am trying to do this



#include <iostream>
#include <iomanip>

//
// if I can adjust the float things then it can be used
//


using namespace std;

int main()
{
      float date;
      date = 12003932;
      cout << setprecision ( 11 ) << date << endl;
      cout << date << endl;
      cout << (date/10000) << endl;
      date = ((date/10000) + 1 ) * 10000;
      
      cout << date << endl;
      
      return 0;
      
}




this is the output




12003932
12003932
1200.3931885
12013932




for some reason this pops up


1200.3931885


when it shouldn't.


how do I fix it?
TroudeloupAsked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
floating numbers are not stored as decimals, indeed them are stored in inverse of the powers of 2, as explained here:
http://en.wikipedia.org/wiki/IEEE_754

So unless you control the display precision by yourself you will have thos unexpected result, try with:
cout << setprecision(4) << (date/10000) << endl;
     
0
 
ozoConnect With a Mentor Commented:
setprecision ( 8 )
or what do you want to happen?
0
 
TroudeloupAuthor Commented:
i just want cout to display the whole float exactly what they are.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
setprecision(4) will show "as you expected", but the number is really 1200.3931885
0
 
ozoConnect With a Mentor Commented:
The exact value, depending on your float implementation, is probably something like
1200.3931884765625
With a double, you could increase the precision to
1200.39319999999997889972291886806488037109375
0
 
ozoConnect With a Mentor Commented:
Or you could use int date, and get exactly
12003932
12003932
1200
12010000
0
 
TroudeloupAuthor Commented:
ok,

float a;

a = 12002323;


cout << a << endl;

should say

1.2002323e7

and

cout << a/10000 << endl;

should say

1.2002323e3





does it work simply like that?


because if the number would change unexpectedly to something else...



0
 
TroudeloupAuthor Commented:
i need to go into decimal values, so I think i can use only float
0
 
ozoConnect With a Mentor Commented:
<< scientific << setprecision ( 8 )
0
 
TroudeloupAuthor Commented:
nice.

can I display float like this


12003939

instead of


1.2003939


?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.