If we get value from edit box as Marathi '१.२' then I want this value as 1.2 in float format, I tried _wtof () but it failed it returned only 0. Its work same as _wtoi (). It's ok but -_wtof ()? Please suggest anything another function.
Asked
Active
Viewed 115 times
0
-
1You could parse the string yourself, I guess. Or if there is a 1 to 1 correspondence of the symbols, just convert the string chars to digits into another string and then parse it with atof. – Adrian Roman Oct 06 '16 at 11:58
-
I assume '१.२' are numbers as written in the Marathi script? – sashoalm Oct 06 '16 at 13:01
-
Thanx Adrian Yes I was parse it but I think it's standard problem therefore asking a any api for conversion of unicode cstring to float value – Yogesh. MFC Oct 06 '16 at 13:06
1 Answers
1
Actually std::stof
and std::stod
work just fine in Visual Studio 2015
#include <iostream>
#include <string>
double convert(const std::wstring src)
{
double f = 0;
try {
f = std::stod(src);
}
catch (...)
{
//error, out of range or invalid string
}
return f;
}
int main()
{
std::cout << std::setprecision(20);
std::cout << convert(L"१२३४५६७८९.१२३४५६७८९") << "\n";
std::cout << convert(L"१.२") << "\n";
return 0;
}
But _wtof
doesn't work. You can use these methods if there is incompatibility in earlier version:
double convert(const std::wstring src)
{
std::wstring map = L"०१२३४५६७८९";
//convert src to Latin:
std::wstring result;
for (auto c : src)
{
int n = map.find(c);
if (n >= 0)
result += (wchar_t)(n+ (int)(L'0'));
else
result += c;
}
double n = 0;
try { n = std::stof(result); }
catch (...)
{
std::wcout << L"error" << result << "\n";
}
return n;
}
int main()
{
std::cout << convert(L"१.१") << "\n";
std::cout << convert(L"१.२") << "\n";
return 0;
}
or using CString
:
double convert(const CString src)
{
CString map = L"०१२३४५६७८९";
//convert src to Latin:
CString result;
for (int i = 0; i < src.GetLength(); i++)
{
int n = map.Find(src[i]);
if (n >= 0)
result += (wchar_t)(n + (int)(L'0'));
else
result += src[i];
}
return _wtof(result.GetString());
}

Barmak Shemirani
- 30,904
- 6
- 40
- 77