Hello -
I am trying to make a program that will calculate some flat sheet metal layouts for me, and am running into trouble because the "double" numbers I am using keep going to the wrong number of decimals. I need to have them exactly what I need or I will not be able to add by 1/10000 of an inch and get good answers. I have Visual Studio 2008 and am working in (learning) C++.
I took the number I need (as a double) and multiplied it be 10000. Then called that an "int" and sent it back to the calling program. Re-typed it as a double here and divided by 10000 to have something with exactly 4 decimals, but no luck.
What else can I do? I have looked on google and can't find help that I can understand.
Any help will be gratefully appreciated.
Thanks,
SmokyRick
Here is the code of my program:
Code:
// tryout4pl_dec.cpp RLC 2011-04-08
// trying to get a true 4 place decimal from a one with more or less digits.
#include <iostream>
#include <cmath>
using std::cin; using std::cout; using std::endl;
// declare functions
double dec_pl(double nmbr, int pwr);
// declare variables
double nmbr = 0.0;
double pwr = 1;
double returned = 0.0;
// here is where the work is done
double dec_pl(double nmbr, double pwr)
{
double number1 = 0.0;
int number2 = 0;
double number3 = 0.0;
double power = pow(10, pwr);
number1 = nmbr * power;
cout << "number1 = nmbr * power = " << number1 << endl;
number2 = (static_cast<int>(number1));
cout << "number2 = (static_cast<int>(number1)) = " << number2 << endl;
number3 = (static_cast<double>(number2)) / power;
cout << "number3 = (static_cast<double>(number2)) / power = " << number3 << endl;
return number3;
}
// and here is main()
int main()
{
cout << "Enter a number to be converted: ";
cin >> nmbr;
cout << "\nEnter a number of decimal places to convert to: ";
cin >> pwr;
returned = dec_pl(nmbr, pwr);
cout << endl << returned << endl << endl;
return 0;
}