Align decimal places in output? - c++

#include <iomanip>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
//
//HERE IS THE ISSUE
//set precision to 3 decimals
cout<<fixed;
//printing the final pressure of the gas
cout <<setw(20)<<left<<setfill('.')<<"Equation #01"<<"Ideal Gas Law(Chemistry): "<<setw(5)<<setprecision(3)<<gaslawPressure<<" atm"
<<endl;
//printing the calculated distance
cout <<setw(20)<<left<<setfill('.')<<"Equation #02"<<"Distance Formula(Math): "<<setw(5)<<setprecision(3)<<pointDistance<<endl;
return 0;
}
Output given:
Equation #01........Ideal Gas Law(Chemistry): 1.641 atm
Equation #02........Distance Formula(Math): 30.017
Output desired:
Equation #01........Ideal Gas Law(Chemistry): 1.641 atm
Equation #02........Distance Formula(Math) : 30.017
I also need to have the colons align as such.

You will have to put proper setw in different parts as well as left align based on your text
1) First part
setw(20)<<left<<setfill('.')<<"Equation #01"
2) Second part assume it to be of approx length 30
setw(30)<<left<<setfill(' ')<<"Ideal Gas Law(Chemistry)"
3) To align colon :
setw(3)<<left<<setfill(' ')<<":"
4) value part
setw(5)<<std::left<<setprecision(3)<<gaslawPressure<<" atm"
#include <iomanip>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
//
//HERE IS THE ISSUE
//set precision to 3 decimals
auto gaslawPressure = 1.641;
auto pointDistance = 30.017;
cout<<fixed;
//printing the final pressure of the gas
cout <<setw(20)<<left<<setfill('.')<<"Equation #01"<<setw(30)<<left<<setfill(' ')<<"Ideal Gas Law(Chemistry)"<<setw(3)<<left<<setfill(' ')<<":"<<setw(5)<<std::left<<setprecision(3)<<gaslawPressure<<" atm"<<endl;
//printing the calculated distance
cout <<std::left<<setw(20)<<left<<setfill('.')<<"Equation #02"<<setw(30)<<left<<setfill(' ')<<"Distance Formula(Math)"<<setw(3)<<left<<setfill(' ')<<":"<<setw(5)<<setprecision(3)<<pointDistance<<endl;
return 0;
}
output
Equation #01........Ideal Gas Law(Chemistry) : 1.641 atm
Equation #02........Distance Formula(Math) : 30.017
Program ended with exit code: 0

UPDATE:
As I saw you didn't want just the second field to align. But if you are hard wiring the fields, you can format those yourself. If passed to you as strings, they can be handled with the same method as your doubles.
As you want to align the decimal points on the results, you have to do that yourself from what I understand. A helper structure keeps it out of the way and reusable.
#include <iomanip>
#include <cmath>
#include <iostream>
struct buf
{
double val;
buf(double val) :val(val) {}
friend std::ostream& operator<< (std::ostream& os, buf b) {
for (double i = b.val; i < 1000; i*=10) os << " ";
return os << b.val;
}
};
int main() {
//
double gaslawPressure = 1.615;
double pointDistance = 221.615;
std::cout << std::setw(20) << std::left << std::setfill('.')
<< "Equation #01" << "Ideal Gas Law(Chemistry) : " << buf(gaslawPressure)<<" atm" << std::endl;
//printing the calculated distance
std::cout << std::setw(20) << std::left << std::setfill('.')
<< "Equation #02" << "Distance Formula(Math) : "<< buf(pointDistance)<< std::endl;
return 0;
}
Output:
Equation #01........Ideal Gas Law(Chemistry) : 1.615 atm
Equation #02........Distance Formula(Math) : 221.615

As far as I know there is no fast way to do that using isstream/iomanip
Precision doesn't define length of fractional part but number of all digits.
I understand, you need to pad values correctly.
In this case, solution is sprintf from [cstdio].
It should look something like this:
sprintf(YourBuffer, "%10.3f", YourVariable);
https://en.cppreference.com/w/cpp/io/c/fprintf
http://www.cplusplus.com/reference/cstdio/printf/ - short version

Related

How do you use setprecision() when declaring a double variable in C++?

So I'm trying to learn more about C++ and I'm practicing by making a calculator class for the quadratic equation. This is the code for it down below.
#include "QuadraticEq.h"
string QuadraticEq::CalculateQuadEq(double a, double b, double c)
{
double sqrtVar = sqrt(pow(b, 2) - (4 * a * c));
double eqPlus = (-b + sqrtVar)/(2 * a);
double eqMinus = (-b - sqrtVar) / (2 * a);
return "Your answers are " + to_string(eqPlus) + " and " + to_string(eqMinus);
}
I'm trying to make it so that the double variables eqPlus and eqMinus have only two decimal points. I've seen people say to use setprecision() but I've only seen people use that function in cout statements and there are none in the class because I'm not printing a string out I'm returning one. So what would I do here? I remember way before learning about some setiosflags() method, is there anything I can do with that?
You can use stringstream instead of the usual std::cout with setprecision().
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
std::string adjustDP(double value, int decimalPlaces) {
// change the number of decimal places in a number
std::stringstream result;
result << std::setprecision(decimalPlaces) << std::fixed << value;
return result.str();
}
int main() {
std::cout << adjustDP(2.25, 1) << std::endl; //2.2
std::cout << adjustDP(0.75, 1) << std::endl; //0.8
std::cout << adjustDP(2.25213, 2) << std::endl; //2.25
std::cout << adjustDP(2.25, 0) << std::endl; //2
}
However, as seen from the output, this approach introduces some rounding off errors when value cannot be represented exactly as a floating point binary number.

My double is rounding up the decimals and I don't want it to

I converted a string to a double using ::atof, it converts OK but it rounds up the decimal and I don't want it to.
string n;
double p;
cout << "String? :" << endl;
cin >> n
p = ::atof(n.c_str());
cout << p << endl;
I usually type in numbers like 123,456.78, 12,345.87, 123,456,789.12. When I type in a smaller number like 1,234.83 or bigger the programs starts messing with the decimals.
It would be of huge help if anybody helps. Thanks!
You need to set the precision used when sending data to the output stream using setprecision as shown below.
Of course the problem with this code is that atof() isn't your best option. However, to answer your question the use of atof() doesn't matter.
#include <iomanip>
#include <iostream>
#include <string>
int main()
{
double x;
std::string n;
std::cout << "String? :" << std::endl;
std::cin >> n;
x = ::atof(n.c_str());
std::cout << std::setprecision(10) << x << std::endl;
}
To convert and catch conversion errors you could use the following:
try
{
x = std::stod(n);
}
catch(std::invalid_argument)
{
// can't convert
}

What is the use of fixed keyword in setprecision while dealing with formatting of numbers in C++?

I have seen a common programming practice to use fixed while using setprecision. Just wanted to know why it is used as I am new to Programming World.
Code in question:
#include <iomanip>
#include <iostream>
int main()
{
double num1 = 3.12345678;
std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(2);
std::cout << num1 << std::endl;
return 0;
}
It is used to clamp the amount of decimal digits to write.
The setprecision(x) call will limit it to x decimals.
More info here:
http://en.cppreference.com/w/cpp/io/manip/setprecision

Long double overflows but value smaller than maximum representable value

I'm trying to compute a series using C++.
The series is:
(for those wondering)
My code is the following:
#include <iostream>
#include <fstream>
#include <cmath> // exp
#include <iomanip> //setprecision, setw
#include <limits> //numeric_limits (http://en.cppreference.com/w/cpp/types/numeric_limits)
long double SminOneCenter(long double gamma)
{
using std::endl; using std::cout;
long double result=0.0l;
for (long double k = 1; k < 1000 ; k++)
{
if(isinf(pow(1.0l+pow(gamma,k),6.0l/4.0l)))
{
cout << "infinity for reached for gamma equals: " << gamma << "value of k: " << k ;
cout << "maximum allowed: " << std::numeric_limits<long double>::max()<< endl;
break;
}
// CAS PAIR: -1^n = 1
if ((int)k%2 == 0)
{
result += pow(4.0l*pow(gamma,k),3.0l/4.0l) /(pow(1+pow(gamma,k)),6.0l/4.0l);
}
// CAS IMPAIR:-1^n = -1
else if ((int)k%2!=0)
{
result -= pow(4.0l*pow(gamma,k),3.0l/4.0l) /(pow(1+pow(gamma,k)),6.0l/4.0l);
//if (!isinf(pow(k,2.0l)*zeta/2.0l))
}
// cout << result << endl;
}
return 1.0l + 2.0l*result;
}
Output will be, for instance with gamma = 1.7 :
infinity reached for gamma equals: 1.7 value of k: 892
The maximum value a long double can represent, as provided by the STL numeric_limits, is: 1.18973e+4932.
However (1+1.7^892)= 2.19.... × 10^308 which is way lower than 10^4932, so it shouldn't be considered as infinity.
Provided my code is not wrong (but it very well might be), could anyone tell me why the discussed code evals to infinity when it should not?
You need to use powl rather than pow if you want to supply long double arguments.
Currently you are hitting the numeric_limits<double>::max() in your pow calls.
As an alternative, consider using std::pow which has appropriate overloads.
Reference http://en.cppreference.com/w/c/numeric/math/pow

float << operation by setting the precision for both integer part and float part

I would like to print floating point numbers in a format like this.
01234.56
I use the following function to round a float to n digits.
double round(double val, int precision)
{
std::stringstream s;
s << std::setprecision(precision) << std::setiosflags(std::ios_base::fixed) << val;
s >> val;
return val;
}
And the following line to output any number with leading zeroes to a fixed length.
setfill ('0') << setw(5) << number
But when I try to combine the two, I end up with non-consistent length, like in the following examples:
8.2 => 008.2
4.57 => 04.56
What I would like to have is an output like this:
8.2 => 08.20
4.57 => 04.57
Can you show me a function like string myround (double d, intpart n, floatpart f) which returns:
myround (1234.5, 5, 2) => 01234.50
myround (1234.569, 5, 2) => 01234.57
I imageine it has been asked before, but I couldn't find it using the internal search engine.
the fixed manipulator will do the trick I think, my example below outputs what you wanted:
#include <iostream>
#include <iomanip>
using namespace std;
int main(void) {
cout << setprecision(2) << setfill ('0') << setw(5) << fixed << 8.2 << endl;
cout << setprecision(2) << setfill ('0') << setw(5) << fixed << 4.57 << endl;
}
just put it into your stringstream and it should work. However, the fixed information will be lost when the string is converted to a double value again. I think it is no good idea to round a float using a string method.