Page 1 of 1

how to set precision for double in C++

Posted: Mon Apr 18, 2016 2:27 pm
by Ktanger
Hi
Please check below MQL4 programming first:

double USOilAskStart = MarketInfo("USOIL", MODE_BID);
double UKOilAskStart = MarketInfo("UKOIL", MODE_BID);
double PriceGapStart = UKOilAskStart - USOilAskStart;
if (NormalizeDouble(PriceGapStart, 3) >= 1.500)
{
...
}
the question is:
I want to find solution in C++ to set precision for double data like MQL4 above "NormalizeDouble(PriceGapStart, 3)" so that i can compare the result, any one can help me?

Re: how to set precision for double in C++

Posted: Mon May 09, 2016 11:49 pm
by violajsilver
You can set the precision directly on std::cout and used the std::fixed format specifier.

Code: Select all

double d = 3.14159265358979;
cout.precision(17);
cout << "Pi: " << fixed << d << endl;

You can #include <limits> to get the maximum precision of a float or double.

Code: Select all

#include <limits>

typedef std::numeric_limits< double > dbl;

double d = 3.14159265358979;
cout.precision(dbl::max_digits10);
cout << "Pi: " << fixed << d << endl;

Re: how to set precision for double in C++

Posted: Wed Aug 31, 2016 4:21 am
by mark1205
std::cout << std::setprecision (std::numeric_limits<double>::digits10 + 1)
<< 3.14159265358979
<< std::endl;

Re: how to set precision for double in C++

Posted: Thu Jan 25, 2018 1:24 am
by Jamesstewart01
thanks for the post

Re: how to set precision for double in C++

Posted: Sun Aug 04, 2019 3:41 am
by zoraya
Thanks for the information!

Re: how to set precision for double in C++

Posted: Tue Mar 31, 2020 6:34 am
by 4waytechnologies
Thanks for the information!

Re: how to set precision for double in C++

Posted: Fri Jul 23, 2021 1:55 am
by GragMilligan
std::cout << std::setprecision (std::numeric_limits<double>::digits10 + 1)
<< 3.14159265358979
<< std::endl;

Its works really. Accepting you really wanted to research articles forming and other making works out, we recommend that you visit some great site, actually look at this. I'm an understudy, yet I oftentimes ask him for help since it saves time and gives me results.

Re: how to set precision for double in C++

Posted: Sun Mar 20, 2022 3:05 am
by catcoin11
Thanks for helping us to your valuable information!

Re: how to set precision for double in C++

Posted: Tue Feb 28, 2023 3:39 am
by AlenVeritaz
Hello! thank you for sharing this info!!