Saturday, December 6, 2014

Set Precision in C++

In this article I would like to share with you a very short and simple program in C++ how to use set precision function in your C++ program. As a programmer using C++ there are some programming problems which deals with decimal values. One of the most interesting solution that I find is the use of C++ library file called iomanip or input / output manipulator. A standard library file in C++ that adds functionality in our program.

Our program will as the user to enter a number and then it will display the number with only two decimal places using the set precision function in C++. If you have some questions regarding about my work in C++ please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

People here in the Philippines can reach me through my mobile number 09173084360 and 09993969756.

Thank you very much and God Bless.



Sample Output of Our Program

Program Listing

#include <iostream>
#include <iomanip>

using namespace std;

main()
{
      float price=0.00;
      cout << setprecision(2) << fixed;
      
      cout << "Set Precision Demo ";
      cout << "\n\n";
      cout << setw(20) << "Enter price : ";
      cin >> price;
      cout << setw(10) <<  "The price is " << price;
      cout << "\n\n";
      system("pause");
}




No comments:

Post a Comment