Converting string to double in C#
There are 3 problems.
1) Incorrect decimal separator
Different cultures use different decimal separators (namely ,
and .
).
If you replace .
with ,
it should work as expected:
Console.WriteLine(Convert.ToDouble("52,8725945"));
You can parse your doubles using overloaded method which takes culture as a second parameter. In this case you can use InvariantCulture
(What is the invariant culture) e.g. using double.Parse
:
double.Parse("52.8725945", System.Globalization.CultureInfo.InvariantCulture);
You should also take a look at double.TryParse
, you can use it with many options and it is especially useful to check wheter or not your string is a valid double
.
2) You have an incorrect double
One of your values is incorrect, because it contains two dots:
15.5859949000000662452.23862099999999
3) Your array has an empty value at the end, which is an incorrect double
You can use overloaded Split
which removes empty values:
string[] someArray = a.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
How do I convert a CString to a double in C++?
A CString
can convert to an LPCTSTR
, which is basically a const char*
(const wchar_t*
in Unicode builds).
Knowing this, you can use atof()
:
CString thestring("13.37");
double d = atof(thestring).
...or for Unicode builds, _wtof()
:
CString thestring(L"13.37");
double d = _wtof(thestring).
...or to support both Unicode and non-Unicode builds...
CString thestring(_T("13.37"));
double d = _tstof(thestring).
(_tstof()
is a macro that expands to either atof()
or _wtof()
based on whether or not _UNICODE
is defined)
C++ string to double conversion
You can convert char to int and viceversa easily because for the machine an int and a char are the same, 8 bits, the only difference comes when they have to be shown in screen, if the number is 65 and is saved as a char, then it will show 'A', if it's saved as a int it will show 65.
With other types things change, because they are stored differently in memory. There's standard function in C that allows you to convert from string to double easily, it's atof. (You need to include stdlib.h)
#include <stdlib.h>
int main()
{
string word;
openfile >> word;
double lol = atof(word.c_str()); /*c_str is needed to convert string to const char*
previously (the function requires it)*/
return 0;
}
Convert double/float to string
The only exact solution is to perform arbitrary-precision decimal arithmetic for the base conversion, since the exact value can be very long - for 80-bit long double
, up to about 10000 decimal places. Fortunately it's "only" up to about 700 places or so for IEEE double
.
Rather than working with individual decimal digits, it's helpful to instead work base-1-billion (the highest power of 10 that fits in a 32-bit integer) and then convert these "base-1-billion digits" to 9 decimal digits each at the end of your computation.
I have a very dense (rather hard to read) but efficient implementation here, under LGPL MIT license:
http://git.musl-libc.org/cgit/musl/blob/src/stdio/vfprintf.c?h=v1.1.6
If you strip out all the hex float support, infinity/nan support, %g
/%f
/%e
variation support, rounding (which will never be needed if you only want exact answers), and other things you might not need, the remaining code is rather simple.
Related Topics
Can a Bool Read/Write Operation Be Not Atomic on X86
How Can an Incomplete Type Be Used as a Template Parameter to Vector Here
C++ Templates, Undefined Reference
Multi-Threading Benchmarking Issues
What Do Each Memory_Order Mean
Is There a Good Python Library That Can Parse C++
Std::Vector, Default Construction, C++11 and Breaking Changes
Why Isn't a For-Loop a Compile-Time Expression
Check If Member Exists Using Enable_If
How to Get the Digits of a Number Without Converting It to a String/ Char Array
Change Default Value of Cmake_Cxx_Flags_Debug and Friends in Cmake
Issue When Scheduling Tasks Using Clock() Function
Understanding Recursion to Generate Permutations
Set Precision of Std::To_String When Converting Floating Point Values