#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream basketFile;
basketFile.open("basket.txt");
double price;
while (!basketFile.eof()) {
basketFile >> price;
cout << price << endl;
}
}
basket.txt
27.9933
18.992
9.754
11.2543
Anyway I can make the numbers appear to only two significant digits? Also, if I want to round a number up how would I do that? For example, if I had the numbers 6.66 and 4.33 I want the 6.66->6.70 and 4.33->4.30. Any help?
Try setprecision.
For rounding a number, see round.
Also, if you decide to round to 0.1 precision, I believe you can just append the zero 0 after the rounded result.
#include <iostream>
#include <iomanip>
using namespace std;
void p(double x) {
cout << fixed << setprecision(1) << x << 0 << endl;
}
int main() {
p(27.9933);
p(18.992);
p(9.754);
p(11.2543);
p(6.66);
p(4.33);
return 0;
}
The code above outputs:
28.00
19.00
9.80
11.30
6.70
4.30
Hope this is what you want.
Related
How to print multiple Zero after the decimal number when I calculate the floating numbers in C++?
I am Using std::cout<<"MEDIA = "<<std::setprecision(6)<<av; to show 5 digits after decimal number. Actually, I am trying to solve URI 1005 (Average 1) problem.
It's working but it's not working when the calculation result is decimal(like: 5,9,10). Then it's not showing the 5 00000 digits after the decimal number.
This is the code.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float A=0;float B=0;
cin>>A;
cin>>B;
float av=((A*3.5)+(B*7.5))/11;
std::cout<<"MEDIA = "<<std::setprecision(6)<<av<<endl;
return 0;
}
Here not getting the 10.00000 if I enter both input 10.0.
Include std::fixed in your output printing:
#include <iomanip>
std::cout << "MEDIA = " << std::fixed << std::setprecision(6) << av << std::endl;
Live Demo
how to print specific number of digits in c++?For example ,printing 8 digits totally(before and after decimal point combined)
Edit: For further clarification, setprecision sets the digits when i have decimal digits to display.I want to display integer 30 also as 30.000000 ,in 8 digits.
The setprecision command puts fixed no. of digits after decimal and i don't want that.
In short , I want an alternative of c command printf("%8d",N) in C++.
You can do it using setprecision() function from include iomanip and fixed like:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double d = 1000;
double t = d;
int dc=0;
while(t>0.9)
{
dc++;
t= t/10;
}
cout<<"dc:"<<dc<<endl;
cout << fixed;
std::cout << std::setprecision(dc);
std::cout << d;
return 0;
}
The setprecision() will not work fine every time So you have to use fixed as well.
You should use the c++ header iomanip what you want is the setprecision() function:
std::cout << std::setprecision(5) << 12.3456789 << std::endl;
outputs 12.346. It also has other modifiers you can find here
EDIT
If you want to print trailing 0s, you need to also use std::fixed. This says to use that number of digits, regardless of whether or not they are significant. If you want that to be the total number, you could figure out the size of the number, then change the precision you set it to based on that, so something like:
#include <iostream>
#include <iomanip>
#include <cmath>
int main()
{
double input = 30;
int magnitude = 0;
while(input / pow(10, magnitude))
{
++magnitude;
}
std::cout << std::fixed << std::setprecision(8 - magnitude) << input << std::endl;
return 0;
}
This returns 30.000000. You can also do something similar by outputting to a string, then displaying that string.
I am trying to separate whole number and decimal number
two int variable
#include <iostream>
#include <iomanip>
#include<math.h>
using namespace std;
int main()
{
double initialAmount = 42800.13;
double fractionalPart = initialAmount - floor(initialAmount);
cout<<int(initialAmount)<<"\n"<<fractionalPart<<"\n";
}
Present output is
42800
0.13
expected output
42800
13
It can be more than two decimal places .
any easy way to do that
Maybe not the best solution, but at least it is simple :)
Store your double initialAmount in a int - which will truncate the decimal part. Then you have the number before the decimal point. Then subtract that from the initial value and you'll be left with just the part after the decimal point. Multiply that by however many powers of 10 that you deem significant and then again store that in a int and you'll have your two values.
As suggested in the comments, it could help to convert the number into a string. Here's one possibility:
#include <sstream>
#include <iostream>
#include <vector>
#include <string>
int main() {
double d = 42800.13;
std::vector<std::string> numbers_s;
std::string s;
std::istringstream numberAsString(std::to_string( d ));
while (getline(numberAsString, s, '.')) {
numbers_s.push_back(s);
}
std::cout << "whole number:" << numbers_s[0] << std::endl;
//[ remove trailing zeros from fractional part:
numbers_s[1].erase(numbers_s[1].find_last_not_of('0') + 1, std::string::npos);
//]
std::cout << "fractional part:" << std::stoi(numbers_s[1]) << std::endl;
}
Error checks should be added to capture cases when the number does not contain a decimal point.
I need a way to convert this string into its exact number representation. I've tried quite a few things and, from what I know, this should work (famous last words). I'm trying to stay away from installing libraries and I have A LOT of these numbers to convert.
I understand that it is difficult to represent long decimal numbers in binary, but I need a solution.
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
string data = "138.6470184568";
cout << atof(data.c_str()) << endl;
return 0;
}
Output
138.647
Turns out that it is converting correctly, but, when asked to cout the number, it only gives 3-4 decimal places. Anyone know why?
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
string data1 = "138.6470184568";
string data2 = "138.6470184568";
double data3 = atof(data1.c_str()) + atof(data2.c_str());
cout << data3 << endl;
cout << setprecision(15) << data3;
return 0;
}
Gives
277.294
277.2940369136
I'm trying to work out the arctan of a number using the formula:
arctan(x) = x - x^3/3 + x^5/5 - x^7/7...
I have to calculate it to 20 decimal places. The answer should be 0.78539....
This is the code I have written, including some debugging statements. The problem is in the calculation I think but I just can't see it. Could someone point me in the right direction please?
EDIT : Can't use the atan function, has to be manually calculated using a double variable from user input.
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
double x;
int i;
int j;
int y=3;
cout<<"Please enter the number you wish to calculate the arctan of:"<<endl;
cin>>x;
//Calculate arctan of this number
cout<<x;
cout<<"\n";
cout<<y;
cout<<"\n";
cout<<"Start\n";
x=x-(pow(x,y)/y);
y=y+2;
cout << setprecision (20) << x;
cout<<"=x before loop\n";
cout<<y;
cout<<"=y before loop\n";
for(i=0;i<9;i++)
{
x=x+(pow(x,y)/y);
cout<<x;
cout<<"=x1 in loop\n";
y=y+2;
cout<<y;
cout<<"=y1 in loop\n";
x-(pow(x,y)/y);
cout<<x;
cout<<"=x2 in loop\n";
y=y+2;
cout<<y;
cout<<"=y2 in loop\n";
}
return 0;
}
Well, your x is changing! You probably want to use a different variable to store the value computed so far and the argument to your function. That said, don't expect to precise outputs because all those computations involve rounding.
This line:
x-(pow(x,y)/y);
might have something to do with your problem.
I would strong advise you use the inbuilt atan function, it is more than likely been well optimised for you architecture, as well as being a standard function recognised by most C++ programmers.
#include <cmath>
#include <iostream>
int main()
{
double d;
std::cout << "enter number" << std::endl;
std::cin >> d;
std::cout << "atan of: " << d
<< " is " << std::atan(d)
<< std::endl;
return 0;
}
I agree with #Mystical. I don't think you're going to get 20 digits of precision out of a double. I think you need a long double (if that exists on your system) or, perhaps you need to implement your own big-num class...