Set precision of std::to_string when converting floating point values [duplicate]

There is no way to change the precision via to_string() but the setprecision IO manipulator could be used instead: #include <sstream> template <typename T> std::string to_string_with_precision(const T a_value, const int n = 6) { std::ostringstream out; out.precision(n); out << std::fixed << a_value; return out.str(); }

Convert double to BigDecimal and set BigDecimal Precision

The reason of such behaviour is that the string that is printed is the exact value – probably not what you expected, but that’s the real value stored in memory – it’s just a limitation of floating point representation. According to javadoc, BigDecimal(double val) constructor behaviour can be unexpected if you don’t take into consideration … Read more

Python Replace \\ with \

There’s no need to use replace for this. What you have is a encoded string (using the string_escape encoding) and you want to decode it: >>> s = r”Escaped\nNewline” >>> print s Escaped\nNewline >>> s.decode(‘string_escape’) ‘Escaped\nNewline’ >>> print s.decode(‘string_escape’) Escaped Newline >>> “a\\nb”.decode(‘string_escape’) ‘a\nb’ In Python 3: >>> import codecs >>> codecs.decode(‘\\n\\x21’, ‘unicode_escape’) ‘\n!’

How to ‘cout’ the correct number of decimal places of a double value?

Due to the fact the float and double are internally stored in binary, the literal 7.40200133400 actually stands for the number 7.40200133400000037653398976544849574565887451171875 …so how much precision do you really want? 🙂 #include <iomanip> int main() { double x = 7.40200133400; std::cout << std::setprecision(51) << x << “\n”; } And yes, this program really prints 7.40200133400000037653398976544849574565887451171875!

Why can’t we use ‘==’ to compare two float or double numbers [duplicate]

From apidoc, Float.compare: Compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the call: new Float(f1).compareTo(new Float(f2)) Float.compareTo: Compares two Float objects numerically. There are two ways in which comparisons performed by this method differ from those performed … Read more

Difference between double and Double in comparison

c and d are technically two different objects and == operator compares only references. c.equals(d) is better as it compares values, not references. But still not ideal. Comparing floating-point values directly should always take some error (epsilon) into account (Math.abs(c – d) < epsilon). Note that: Integer c = 1; Integer d = 1; here … Read more

How to round a Double to the nearest Int in swift?

There is a round available in the Foundation library (it’s actually in Darwin, but Foundation imports Darwin and most of the time you’ll want to use Foundation instead of using Darwin directly). import Foundation users = round(users) Running your code in a playground and then calling: print(round(users)) Outputs: 15.0 round() always rounds up when the … Read more

tech