You are here

Handling Floating-point Data Type

10 February, 2015 - 12:49

It is nice to have your output displayed so humans can read it (most humans are not use to scientifc notation). There are three lines often inserted near the start of your code (first items in the function main) that can be used to direct the formatting of floating-point data. They are:

cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(n); 

They do the following for the rest of your program:

  • fixed - Do not use scientifc notation but show floating-point values like integer values (numeral digits of 0 to 9 no exponent notation).
  • showpoint - Always show a decimal point for floating-point values even if there is no fractional part.
  • precision - Always show this number of digits (change n to a number like 2) to the right of the decimal point.