I am trying to print the average in Cpp up to 2 decimal points of the float num. avg is float, sum is float, count is int.
Currently if I have 10/1 for example it outputs 10.00. I want the output to be just 10. if avg gets value 3.1467665 for example, it should be displayed as 3.14.
avg = sum/count;
std::cout << std::fixed << std::setprecision(2) << avg;
The rounding should be just for the output. No need to change avg but if it is easier, its value can be changed.
Looking for a solution using a standard before c++11.
UPD: the output is 27.50 when I want it to be 27.5.
You could choose precision according to the floating modulus of the avg value. The following works:
int main() {
double sum = 10;
double count = 3;
double avg = sum/count;
double mod;
std::cout << std::fixed
<< std::setprecision((modf(avg, &mod) != 0.0) ? 2 : 0)
<< avg
<< std::endl;
}
Considering the added specifications:
Write 2.5 instead of 2.50
Write 3.14 for 3.1421783921, rather than 3.15
Here is a possible implementation using #IInspectable's suggested method:
std::stringstream ss;
ss << std::fixed << avg;
size_t pos = ss.str().find('.');
if (pos + 2 < ss.str().size()) // If there is a '.' and more than 2 digits after it
pos += 3;
std::string s = ss.str().substr(0, pos); // Leave only two digits
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return ch != '0'; }).base(), s.end()); // Remove trailing zeros
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return ch != '.'; }).base(), s.end()); // Remove trailing '.' when needed
std::cout << s << std::endl;
This will output:
10/4 -> 2.5
10/3 -> 3.33
10/2 -> 5
10/7 -> 1.42
3.9999 -> 3.99
Related
Precision is the number of digits in a number. Scale is the number of
digits to the right of the decimal point in a number. For example, the
number 123.45 has a precision of 5 and a scale of 2.
I need to convert a double with a maximum scale of 7(i.e. it may have 7 digits after the decimal point) to a __int128. However, given a number, I don't know in advance, the actual scale the number has.
#include <iostream>
#include "json.hpp"
using json = nlohmann::json;
#include <string>
static std::ostream& operator<<(std::ostream& o, const __int128& x) {
if (x == std::numeric_limits<__int128>::min()) return o << "-170141183460469231731687303715884105728";
if (x < 0) return o << "-" << -x;
if (x < 10) return o << (char)(x + '0');
return o << x / 10 << (char)(x % 10 + '0');
}
int main()
{
std::string str = R"({"time": [0.143]})";
std::cout << "input: " << str << std::endl;
json j = json::parse(str);
std::cout << "output: " << j.dump(4) << std::endl;
double d = j["time"][0].get<double>();
__int128_t d_128_bad = d * 10000000;
__int128_t d_128_good = __int128(d * 1000) * 10000;
std::cout << std::setprecision(16) << std::defaultfloat << d << std::endl;
std::cout << "d_128_bad: " << d_128_bad << std::endl;
std::cout << "d_128_good: " << d_128_good << std::endl;
}
Output:
input: {"time": [0.143]}
output: {
"time": [
0.143
]
}
0.143
d_128_bad: 1429999
d_128_good: 1430000
As you can see, the converted double is not the expected 1430000 instead it is 1429999. I know the reason is that a float point number can not be represented exactly. The problem can be solved if I know the number of digit after the decimal point.
For example,
I can instead use __int128_t(d * 1000) * 10000. However, I don't know the scale of a given number which might have a maximum of scale 7.
Question> Is there a possible solution for this? Also, I need to do this conversion very fast.
I'm not familiar with this library, but it does appear to have a mechanism to get a json object's string representation (dump()). I would suggest you parse that into your value rather than going through the double intermediate representation, as in that case you will know the scale of the value as it was written.
I have always found iomanip confusing and counter intuitive. I need help.
A quick internet search finds (https://www.vedantu.com/maths/precision) "We thus consider precision as the maximum number of significant digits after the decimal point in a decimal number" (the emphasis is mine). That matches my understanding too. However I wrote a test program and:
stm << std::setprecision(3) << 5.12345678;
std::cout << "5.12345678: " << stm.str() << std::endl;
stm.str("");
stm << std::setprecision(3) << 25.12345678;
std::cout << "25.12345678: " << stm.str() << std::endl;
stm.str("");
stm << std::setprecision(3) << 5.1;
std::cout << "5.1: " << stm.str() << std::endl;
stm.str("");
outputs:
5.12345678: 5.12
25.12345678: 25.1
5.1: 5.1
If the precision is 3 then the output should be:
5.12345678: 5.123
25.12345678: 25.123
5.1: 5.1
Clearly the C++ standard has a different interpretation of the meaning of "precision" as relates to floating point numbers.
If I do:
stm.setf(std::ios::fixed, std::ios::floatfield);
then the first two values are formatted correctly, but the last comes out as 5.100.
How do I set the precision without padding?
You can try using this workaround:
decltype(std::setprecision(1)) setp(double number, int p) {
int e = static_cast<int>(std::abs(number));
e = e != 0? static_cast<int>(std::log10(e)) + 1 + p : p;
while(number != 0.0 && static_cast<int>(number*=10) == 0 && e > 1)
e--; // for numbers like 0.001: those zeros are not treated as digits by setprecision.
return std::setprecision(e);
}
And then:
auto v = 5.12345678;
stm << setp(v, 3) << v;
Another more verbose and elegant solution is to create a struct like this:
struct __setp {
double number;
bool fixed = false;
int prec;
};
std::ostream& operator<<(std::ostream& os, const __setp& obj)
{
if(obj.fixed)
os << std::fixed;
else os << std::defaultfloat;
os.precision(obj.prec);
os << obj.number; // comment this if you do not want to print immediately the number
return os;
}
__setp setp(double number, int p) {
__setp setter;
setter.number = number;
int e = static_cast<int>(std::abs(number));
e = e != 0? static_cast<int>(std::log10(e)) + 1 + p : p;
while(number != 0.0 && static_cast<int>(number*=10) == 0)
e--; // for numbers like 0.001: those zeros are not treated as digits by setprecision.
if(e <= 0) {
setter.fixed = true;
setter.prec = 1;
} else
setter.prec = e;
return setter;
}
Using it like this:
auto v = 5.12345678;
stm << setp(v, 3);
I don't think you can do it nicely. There are two candidate formats: defaultfloat and fixed. For the former, "precision" is the maximum number of digits, where both sides of the decimal separator count. For the latter "precision" is the exact number of digits after the decimal separator.
So your solution, I think, is to use fixed format and then manually clear trailing zeros:
#include <iostream>
#include <iomanip>
#include <sstream>
void print(const double number)
{
std::ostringstream stream;
stream << std::fixed << std::setprecision(3) << number;
auto string=stream.str();
while(string.back()=='0')
string.pop_back();
if(string.back()=='.') // in case number is integral; beware of localization issues
string.pop_back();
std::cout << string << "\n";
}
int main()
{
print(5.12345678);
print(25.12345678);
print(5.1);
}
The fixed format gives almost what you want except that it preserves trailing zeros. There is no built-in way to avoid that but you can easily remove those zeros manually. For example, in C++20 you can do the following using std::format:
std::string format_fixed(double d) {
auto s = fmt::format("{:.3f}", d);
auto end = s.find_last_not_of('0');
return end != std::string::npos ? std::string(s.c_str(), end + 1) : s;
}
std::cout << "5.12345678: " << format_fixed(5.12345678) << "\n";
std::cout << "25.12345678: " << format_fixed(25.12345678) << "\n";
std::cout << "5.1: " << format_fixed(5.1) << "\n";
Output:
5.12345678: 5.123
25.12345678: 25.123
5.1: 5.1
The same example with the {fmt} library, std::format is based on: godbolt.
Disclaimer: I'm the author of {fmt} and C++20 std::format.
I'm trying to print a max of 4 digits after the decimal point in C++ (Using streams). So if the number does not need 4 digits after the decimal I want it to use only the number of decimals it actually needs.
Examples:
1.12345 -> 1.1234
1.0 -> 1
1.12 -> 1.12
1.12345789 -> 1.1234
123.123 -> 123.123
123.123456 -> 123.1234
I tried std::setprecision(4) but that sets the number of significant digits and fails in the test case:
123.123456 gives 123.1
I also tried giving std::fixed along with std::setprecision(4) but that gives a fixed number of digits after decimal even if not needed:
1.0 gives 1.0000
It seems like std::defaultfloat is the one I need and not fixed nor exponential. But it does not seem to be printing the number of digits after the decimal appropriately and only has an option for significant digits.
We can do this using a std::stringstream and a std::string. We pass the double to the stream formatting it like we would if we are sending it to cout. Then we examine the string we get from the stream to see if there are trailing zeros. If there are we get rid of them. Once we do that we check to see if we are left with just a decimal point, if we are then we get rid of that as well. You could use something like this:
int main()
{
double values[] = { 1.12345, 1.0, 1.12, 1.12345789, 123.123, 123.123456, 123456789, 123.001 };
std::vector<std::string> converted;
for (auto e : values)
{
std::stringstream ss;
ss << std::fixed << std::setprecision(4) << e;
std::string value(ss.str());
if (value.find(".") != std::string::npos)
{
// erase trailing zeros
while (value.back() == '0')
value.erase(value.end() - 1);
// if we are left with a . at the end then get rid of it
if (value.back() == '.')
value.erase(value.end() - 1);
converted.push_back(value);
}
else
converted.push_back(value);
}
for (const auto& e : converted)
std::cout << e << "\n";
}
Which when made into a running example gives
1.1235
1
1.12
1.1235
123.123
123.1235
123456789
123.001
Using the answer from here along with custom logic to remove the zeros and the point:
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
std::string remove_zeros(std::string numberstring)
{
auto it = numberstring.end() - 1;
while(*it == '0') {
numberstring.erase(it);
it = numberstring.end() - 1;
}
if(*it == '.') numberstring.erase(it);
return numberstring;
}
std::string convert(float number)
{
std::stringstream ss{};
ss << std::setprecision(4) << std::fixed << std::showpoint << number;
std::string numberstring{ss.str()};
return remove_zeros(numberstring);
}
int main()
{
const float values[]{1.12345, 1.0, 1.12, 1.12345789, 147323.123, 123.123456};
for(auto i : values)
std::cout << convert(i) << '\n';
}
produces:
1.1235
1
1.12
1.1235
147323.125
123.1235
Let's say that you have a function:
string function(){
double f = 2.48452
double g = 2
double h = 5482.48552
double i = -78.00
double j = 2.10
return x; // ***
}
* for x we insert:
if we will insert f, function returns: 2.48
if we will insert g, function returns: 2
if we will insert h, function returns: 5482.49
if we will insert i, function returns:-78
if we will insert j, function returns: 2.1
They are only example, who shows how the funcion() works. To precise:
The function for double k return rounded it to: k.XX,
but for:
k=2.20
it return 2.2 as string.
How it implements?
1) Just because you see two digits, it doesn't mean the underlying value was necessarily rounded to two digits.
The precision of the VALUE and the number of digits displayed in the FORMATTED OUTPUT are two completely different things.
2) If you're using cout, you can control formatting with "setprecision()":
http://www.cplusplus.com/reference/iomanip/setprecision/
EXAMPLE (from the above link):
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
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';
return 0;
}
sample output:
3.1416
3.14159
3.14159
3.141590000
Mathematically, 2.2 is exactly the same as 2.20, 2.200, 2.2000, and so on. If you want to see more insignificant zeros, use [setprecision][1]:
cout << fixed << setprecision(2);
cout << 2.2 << endl; // Prints 2.20
To show up to 2 decimal places, but not showing trailing zeros you can do something such as:
std::string function(double value)
{
// get fractional part
double fracpart = value - static_cast<long>(value);
// compute fractional part rounded to 2 decimal places as an int
int decimal = static_cast<int>(100*fabs(fracpart) + 0.5);
if (decimal >= 100) decimal -= 100;
// adjust precision based on the number of trailing zeros
int precision = 2; // default 2 digits precision
if (0 == decimal) precision = 0; // 2 trailing zeros, don't show decimals
else if (0 == (decimal % 10)) precision = 1; // 1 trailing zero, keep 1 decimal place
// convert value to string
std::stringstream str;
str << std::fixed << std::setprecision(precision) << value;
return str.str();
}
I have a double (or float) number x:
x = 1234.5678;
Now, the question is, how do break down the number into 2 int's whereas int1 would get the number before the point, and int2 is the number after the point.
The first part is easy, which we can either cast, or take a round or ceiling to get the first part into an int, I am looking for the second part to become int2=5678 without any floating points there.
i.e. to to extend the above example:
float x = 1234.5678;
int x1 = (int) x; // which would return 1234 great.
int x2 = SomeFunction????(x); // where I need x2 to become = 5678
Notice the 5678 should not have any points there.
It would be nice to hear from you.
Thanks
Heider
Here are two ways of doing it.
The first one uses std::stringstream, std::string and std::strtol and is sorta hacky. It is also not very efficient, but it does the job.
The second one needs to know the number of decimals and uses simple multiplication. NOTE: This method will not do any kind of checking on whether the float you pass in actually has that number of decimals.
None of these methods are particularly elegant, but they worked well for the numbers I tested ( both positive and negative. ) Feel free to comment if you find bugs/errors or if you have suggestions for improvement.
EDIT : As #dan04 pointed out, this method will return the same value for 0.4 as for 0.04. If you want do distinguish these, you'd need a second int for storing the number of zeros after the decimal point.
#include <iostream>
#include <sstream>
#include <math.h>
int GetDecimalsUsingString( float number );
int GetDecimals( float number, int num_decimals );
int main() {
float x = 1234.5678;
int x1 = (int) x; // which would return 1234 great.
float remainder = x - static_cast< float > ( x1 );
std::cout << "Original : " << x << std::endl;
std::cout << "Before comma : " << x1 << std::endl;
std::cout << "Remainder : " << remainder << std::endl;
// "Ugly" way using std::stringstream and std::string
int res_string = GetDecimalsUsingString( remainder );
// Nicer, but requires that you specify number of decimals
int res_num_decimals = GetDecimals( remainder, 5 );
std::cout << "Result using string : " << res_string << std::endl;
std::cout << "Result using known number of decimals : " << res_num_decimals << std::endl;
return 0;
}
int GetDecimalsUsingString( float number )
{
// Put number in a stringstream
std::stringstream ss;
ss << number;
// Put content of stringstream into a string
std::string str = ss.str();
// Remove the first part of the string ( minus symbol, 0 and decimal point)
if ( number < 0.0 )
str = str.substr( 3, str.length() - 1);
else
str = str.substr( 2, str.length() - 1);
// Convert string back to int
int ret = std::strtol( str.c_str(), NULL, 10 );
/// Preserve sign
if ( number < 0 )
ret *= -1;
return ret;
}
int GetDecimals( float number, int num_decimals )
{
int decimal_multiplier = pow( 10, num_decimals );
int result = number * decimal_multiplier;
return result;
}
Output :
Original : 1234.57
Before comma : 1234
Remainder : 0.567749
Result using string : 567749
Result using known number of decimals : 56774
Ideone
I guess there are no built in C/C++ commands to do this, other than the 2 methods of:
1) Using the above to convert into string and then scan back into 2 ints.
2) Accessing the memory contents of the memory variable and then decoding manually.