将马拉地语(非阿拉伯语)数字转换为阿拉伯数字

时间:2016-10-06 11:52:23

标签: c++ number-systems

如果我们从编辑框中获得价值,就像Marathi' 1.2'然后我希望这个值为浮点格式的1.2,我尝试_wtof()但它失败了它只返回0.它的工作与_wtoi()相同。没关系,但-_wtof()?请建议其他任何功能。

1 个答案:

答案 0 :(得分:1)

实际上,std::stofstd::stod在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;
}

_wtof不起作用。如果早期版本中存在不兼容性,则可以使用这些方法:

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;
}

或使用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());
}
相关问题