I'm creating a calculator program, but I don't know how to set the precision of the decimal places of the calculated answer so that the actual number of decimal places is displayed.
Examples:
-If the answer is 6, it would display 6, not 6.0000000
-if the answer is 8.23, it would display 8.23, not 8.23000000
etc.
How would I set the precision as such?
In C++, use the method precision to set the number of needed placeholders, like:
double f = 3.14159;
std::cout.unsetf ( std::ios::floatfield ); // floatfield not set
std::cout.precision(5); //will output 5 digits after point
std::cout << f << '\n';
std::cout.precision(10); //will output 10 digits after point, like 3.1415900000
std::cout << f << '\n';
std::cout.setf( std::ios::fixed, std:: ios::floatfield ); // floatfield set to fixed
std::cout << f << '\n';
To remove any trailing zeroes, you can use
std::cout.precision(0);
Basically, you can set:
std::cout.precision(6); // 6 digits
or whatever value you need. Or:
std::cout << std::setprecision(5) << my_double_value << std::endl;
iostream provides a setprecision function:
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n'
Output will be:
3.1416
3.14159
3.14159
3.141590000
There isn't an "actual number of decimal places". Double precision has a number of binary places but these don't map reliably into a number of decimal places. An easy example is 1.0/10,0 which should be 0.1 but is actually a number quite close to 0.1; how many decimal places you show for sums such as these is thus not a quality of the double itself but rather of how many decimal places you want to show.
How you can truncate to the number you want has been discussed in the other answers.
This will work:
printf("%g",value);
Related
double calc_percent_of_daily_sugar(int sugar) {
double result;
result = sugar/avg_sugar_intake;
return result*100;
}
I'm trying to get this function to return the result to only 2 decimal places however when I try to use cout << setprecision(2) << result; then there are still more than 2 places in the number
Apologies if this is a nooby or stupid question.
*How to print a “double” value to two decimal places in base 10 in C++
Use std::fixed to print it in decimal.
std::cout << std::setprecision(2) << std::fixed << 123.4567 << std::endl;
// 123.46
See: https://en.cppreference.com/w/cpp/io/manip/fixed
I am looking for a way to convert double to string in c++ such that total number of digits remain to be 10 irrespective of how many are present before and after the decimal point and irrespective of the zeroes. Examples for better understanding:
0.00000000000000000000 =expected outcome> 0.0000000000
12345.00000000000000000 =expected outcome> 12345.00000
-15.123456789012 =expected outcome> -15.12345678
I couldnt find any relevant answer. For the methods like snprintf, std::setprecision with ostringstream, to_string, Boost's lexical_cast, some of the above case fails.
Example of code:
double num = 12345.0000000000001;
std::ostringstream streamObj2;
streamObj2 << std::fixed << std::setprecision(10) << num;
std::string strObj2 = streamObj2.str();
std::cout << strObj2 << '\n';
The output = 12345.0000000000, which is not what I am expecting. Removing std::fixed gives output as 12345
What I require = 12345.00000
Please help me, thanks.
Thanks for the comments, because of which I found the answer. Using std::showpoint instead of std::fixed helped fulfill the requirement.
double num = 12345.0000000000001;
std::ostringstream streamObj2;
streamObj2 << std::showpoint << std::setprecision(10) << num;
std::string strObj2 = streamObj2.str();
std::cout << strObj2 << '\n';
Output: 12345.00000
I am getting an issue when trying to output my float using std::cout <<
I have the following values:
vector2f = {-32.00234098f, 96.129380f} //takes 2 floats (x, y)
output: -32.0023:96.1294
What I am looking for is:
output: -32.00234098:96.129380
The actual numbers could be vary from the 7 decimal places (.0000007) to 3 decimal places (.003) so setting a fixed rounding number does not work in this case.
Any help would be great as I have tried changed to doubles as well but to no avail.
Thanks in advance!
There are 2 problems.
you need to include <iomanip> and use the std::setprecision manipulator.
To get the level of accuracy you want you will need to use doubles rather than floats.
e.g.:
#include <iostream>
#include <iomanip>
int main()
{
auto x = -32.00234098f, y = 96.129380f;
std::cout << std::setprecision(8) << std::fixed << x << ":" << y << std::endl;
// doubles
auto a = -32.00234098, b = 96.129380;
std::cout << std::setprecision(8) << std::fixed << a << ":" << b << std::endl;
}
example output:
-32.00234222:96.12937927
-32.00234098:96.12938000
You can set the output precision of the stream using std::precision manipulator.
To print trailing zeroes up to the given precision like in your example output, you need to use std::fixed manipulator.
I'm using visual studio 2015 to print two floating numbers:
double d1 = 1.5;
double d2 = 123456.789;
std::cout << "value1: " << d1 << std::endl;
std::cout << "value2: " << d2 << std::endl;
std::cout << "maximum number of significant decimal digits (value1): " << -std::log10(std::nextafter(d1, std::numeric_limits<double>::max()) - d1) << std::endl;
std::cout << "maximum number of significant decimal digits (value2): " << -std::log10(std::nextafter(d2, std::numeric_limits<double>::max()) - d2) << std::endl;
This prints the following:
value1: 1.5
value2: 123457
maximum number of significant decimal digits (value1): 15.6536
maximum number of significant decimal digits (value2): 10.8371
Why 123457 is printed out for the value 123456.789? Does ANSI C++ specification allow to display anything for floating numbers when std::cout is used without std::setprecision()?
The rounding off happens because of the C++ standard which can be seen by writing
std::cout<<std::cout.precision();
The output screen will show 6 which tells that the default number of significant digits which will be printed by the std::cout statement is 6. That is why it automatically rounds off the floating number to 6 digits.
What you have have pointed out is actually one of those many things that the standardization committee should consider regarding the standard iostream in C++. Such things work well when you write :-
printf ("%f\n", d2);
But not with std::cout where you need to use std::setprecision because it's formatting is similar to the use of %g instead of %f in printf. So you need to write :-
std::cout << std::setprecision(10) << "value2: " << d2 << std::endl;
But if you dont like this method & are using C++11 (& onwards) then you can also write :-
std::cout << "value2: " << std::to_string(d2) << std::endl;
This will give you the same result as printf ("%f\n", d2);.
A much better method is to cancel the rounding that occurs in std::cout by using std::fixed :-
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::fixed;
double d = 123456.789;
std::cout << d;
return 0;
}
Output :-
123456.789000
So I guess your problem is solved !!
I think the problem here is that the C++ standard is not written to be easy to read, it is written to be precise and not repeat itself. So if you look up the operator<<(double), it doesn't say anything other than "it uses num_put - because that is how the cout << some_float_value is implemented.
The default behaviour is what print("%g", value); does [table 88 in n3337 version of the C++ standard explains what the equivalence of printf and c++ formatting]. So if you want to do %.16g you need to change the precision by calling setprecision(16).
As I understand it the setprecision function specifies the minimal precision but when I run the following code I get only 3 numbers after the decimal point:
int main()
{
double a = 123.4567890;
double b = 123.4000000;
std::cout << std::setprecision(5) << a << std::endl; // Udesireble
std::cout.setf(std::ios::fixed);
std::cout << std::setprecision(5) << a << std::endl; // Desireble
std::cout << std::setprecision(5) << b << std::endl; // Udesireble
std::cout.unsetf(std::ios::fixed);
std::cout << std::setprecision(5) << b << std::endl; // Desireble
return 0;
}
which prints:
123.46 // Udesireble
123.45679 // Desireble
123.40000 // Udesireble
123.4 // Desireble
Is there any way I can avoid checking the number of digits after the decimal point myself in order to know whether to set fixed ?
My impression is that you will need to format to string first, and then replace trailing zeros with spaces.
For the streams, you can use two functions.
setfill(char_type c), which set the character to write, to match with the number of needed character (more information here)
There is the setw(int) function, which set the width of field of the value to display. (documentation here )
Using these functions, you may have a solution