I have a code where i can get digit upto 9 digits after decimal point so say something like 0.123456789. Now we can have a case where i get the value 10.123 or say 1001.12. Now there are only 3 digits after decimal point and 2 digits in e.g 10.123 and 1001.12. I am using
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
using namespace std;
int main()
{
std:stringstream ss;
double val = 1.234;
ss.str(std::string());
ss << std::fixed;
ss << std::setprecision(9);
ss << val;
string number= ss.str();
std::cout << number <<"\n";
return 0;
}
Above would give output as 1.234000000 . Note that i would want the precision to be handled automatically depending on the length of the digits after decimal point. One way is for me to find number of digits after decimal point and set precision evverytime , is there some other standard method provided, that takes care of it ?
Thanks
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
I've used setprecision(8), but when I count asin(1) it returns 0.017452406 (10 digits, not 8!) What can be done there?
In order to fix the problem with decimal digits you need to use std::fixed.
As explaned in C++ Reference
Sets the floatfield format flag for the str stream to fixed.
When floatfield is set to fixed, floating-point values are written using fixed-point notation: the value is represented with exactly as many digits in the decimal part as specified by the precision field (precision) and with no exponent part.
To answer your requirement from the comments: "I need to make number (both integer and decimal parts) to be displayed in max 8 digits." You could use an ostringstream.
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
int main(){
std::ostringstream ss; // declare your string stream
ss << std::setprecision(16); // set a high precision to avoid rounding
// Insert your float to the string stream
float f = 0.123456789;
ss << f;
// Get the string from the stream
std::string f_str = ss.str();
// Print
std::cout << f_str.substr(0, 8 + 1) << std::endl; // take 8 first digits
// Note the +1 here ^ (because of the dot)
return 0;
}
Output
0.1234567
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 see many questions about the precision number for floating point numbers but specifically I want to know why this code
#include <iostream>
#include <stdlib.h>
int main()
{
int a = 5;
int b = 10;
std::cout.precision(4);
std::cout << (float)a/(float)b << "\n";
return 0;
}
shows 0.5? I expect to see 0.5000.
Is it because of the original integer data types?
#include <iostream>
#include <stdlib.h>
#include <iomanip>
int main()
{
int a = 5;
int b = 10;
std::cout << std::fixed;
std::cout << std::setprecision(4);
std::cout << (float)a/(float)b << "\n";
return 0;
}
You need to pass std::fixed manipulator to cout in order to show trailing zeroes.
std::cout.precision(4); tells the maximum number of digits to use not the minimum.
that means, for example, if you use
precision 4 on 1.23456 you get 1.235
precision 5 on 1.23456 you get 1.2346
If you want to get n digits at all times you would have to use std::fixed.
The behavior is correct. The argument specifies the maximum meaningful amount of digits to use. It is not a minimum. If only zeroes follow, they are not printed because zeroes at the end of a decimal part are not meaningful. If you want the zeroes printed, then you need to set appropriate flags:
std::cout.setf(std::ios::fixed, std::ios::floatfield);
This sets the notation to "fixed", which prints all digits.