Problem with strtold

Hi,
I have a problem with converting a C++ string into a long double. In order to do this, I used the function strtold, but the result I get is only the integral part, that is: if for example the input string is 12.476, I only get 12 as the converted long double value. This happens with atof too.
Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*This is the include section*/
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sstream>

/* Code giving problems */

string test = "12.345";
long double test_longd = strtold(test.c_str(),NULL);

/*The following is because I have to write the result in a WxWidgets component*/

ostringstream parte_dec_oss;
parte_dec_oss << testing;
WxRichTextCtrl1->WriteText(parte_dec_oss.str()); 


Could you help me? I can't figure out how to solve this issue.
Thanks.
Not sure what the last 3 lines are doing. Shouldn't test_longd be used here somewhere?
If you are using template class std::string then it is better to use std::stold. For example

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
	std::string s = "12.345";
	std::cout << std::stold( s ) << std::endl;

	return 0;
}
Your code snippet is fine, if compiled as

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <cstdlib>
#include <cstdio>

int main()
{
    std::string test = "12.345";
    long double test_longd = std::strtold(test.c_str(), NULL);
    std::printf("%Lf\n", test_longd);
}



$ ./test
12.345000


Post a compilable example that reproduces the problem.

(One way to make it do what you describe is to load a locale that uses something other than '.' for decimal separator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <cstdlib>
#include <cstdio>
#include <clocale>

int main()
{
    std::string test = "12.345";
    if(std::setlocale(LC_ALL, "de_DE"))
    {
        long double test_longd = std::strtold(test.c_str(), NULL);
        std::printf("%Lf\n", test_longd);
    }
}


$ ./test
12,000000

Topic archived. No new replies allowed.