C# Converting 20 digit precision double to string and back again

Use the “R” numeric format string: double d = 0.00034101243963859839; string s = d.ToString(“R”); //… double d2 = double.Parse(s); if(d == d2) { //– Success } The R stands for “round-trip”. From the linked document: This format is supported only for the Single and Double types. The round-trip specifier guarantees that a numeric value converted … Read more

Should we generally use float literals for floats instead of the simpler double literals?

Yes, you should use the f suffix. Reasons include: Performance. When you write float foo(float x) { return x*3.14; }, you force the compiler to emit code that converts x to double, then does the multiplication, then converts the result back to single. If you add the f suffix, then both conversions are eliminated. On … Read more

Java correct way convert/cast object to Double

new Double(object.toString()); But it seems weird to me that you’re going from an Object to a Double. You should have a better idea what class of object you’re starting with before attempting a conversion. You might have a bit of a code quality problem there. Note that this is a conversion, not casting.

C# double to decimal precision loss

138630.78380386264 is not exactly representable to double precision. The closest double precision number (as found here) is 138630.783803862635977566242218017578125, which agrees with your findings. You ask why the conversion to decimal does not contain more precision. The documentation for Convert.ToDecimal() has the answer: The Decimal value returned by this method contains a maximum of 15 significant … Read more

Sum up ArrayList via Java Stream [duplicate]

The mapToDouble call is not useless: it performs an implicit unboxing. Actually it’s the same as double totalevent = myList.stream().mapToDouble(f -> f.doubleValue()).sum(); Or double totalevent = myList.stream().mapToDouble(Double::doubleValue).sum(); Alternatively you can use summingDouble collector, but it’s not a big difference: double totalevent = myList.stream().collect(summingDouble(f -> f)); In my StreamEx library you can construct a DoubleStream directly … Read more

How to convert a std::string to double

#include <iostream> #include <string> using namespace std; int main() { cout << stod(” 99.999 “) << endl; } Output: 99.999 (which is double, whitespace was automatically stripped) Since C++11 converting string to floating-point values (like double) is available with functions: stof – convert str to a float stod – convert str to a double stold … Read more

tech