繁体   English   中英

如何使用 C++ 或任何其他 function 中的 _atold() function 将 C-String 转换为 long double?

[英]how can I convert a C-String into long double using _atold() function in C++ or any other function?

// 请参阅下面的代码并帮助我为什么我没有得到正确的结果。 或建议任何其他 C++ function 将“$ 567,789,675.89”之类的C字符串转换为长双

  long double mstold( char s[] )
{
    int len = strlen(s);
    long double cash;
    int n=0;
    char amount[ 100 ];
for( int i=0; i<len; i++)                       // for copying the passed C-String into another C-string
  {
    amount[n] = s[i];
      n++;
    if( s[i] == '$' || s[i] == ',')     // Because the C-String has been passed in format: "$567,789,564.987"
        n--;
  }

   cash = _atold( amount );           // This does not gives the right result
   return cash;
 }

使用 strtold() function,因为 _atold() 是非标准的 function。 我正在发布在编译器资源管理器中工作的代码。 您没有用 '\0' 终止amount数组。 也许这就是_atold不起作用的原因。

#include <cstdlib>
#include <iostream>
#include <cstring>


using namespace std;

long double mstold(const char* s)
{
    int len = strlen(s);
    long double cash;
    int n = 0;
    char* amount = new char[len+1];
    for (int i = 0; i<len; i++)                       // for copying the passed C-String into another C-string
    {
        amount[n] = s[i];
        n++;
        if (s[i] == '$' || s[i] == ',')     // Because the C-String has been passed in format: "$567,789,564.987"
            n--;
    }
    amount[n] = '\0';
    cash = strtold(amount, NULL);           // This does not gives the right result
    delete[] amount;
    return cash;
}

int main()
{
    long double cash = mstold("$567,789,675.89");
    std::cout << cash << std::endl;
}

第一个注释。 请不要使用 C-Style 字符串。 在 C++ 我们使用std::string 无论如何,C 风格的字符串也可以并且可以自动转换。

然后,对于新手来说,最好将输入的货币字符串转换为只有一位十进制数字的数字字符串,然后使用 function stold进行转换。 你可以在这里读到它。

但在真正的 C++ 世界中,你会做两件事:

  • 使用专用的 C++ 设施
  • 使用本地化

不幸的是,这是一个相当复杂的话题,您需要一段时间才能理解。

您需要阅读本地化库。 在这里,您将了解 2 个主要概念:

  1. 语言环境
  2. 刻面

一般来说,日期、货币价值或数字格式的文本表示受区域或文化惯例的约束。 所有这些属性都包含在std::locale object 中。

但是std::locale并没有提供太多功能。 真正的本地化工具以刻面的形式提供。 std::locale封装了几个方面。 其中之一是关于货币格式。

你真的可以用它做很多事情,并最终获得完全定制的行为。 但是,如前所述,并不那么容易理解。

我将在下面的示例中使用std::money_get class。

请注意。 这会将您的数字转换为单位,没有分数。 在财务计算中,我们基本上不应该使用分数,因为double甚至long double无法存储所有“真实”值”。请同时阅读。

反正。 我将向您展示如何在 C++ 中转换这种货币值的示例。 您可能会对复杂性感到震惊,但灵活性是有代价的。 . .

请参见:

#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <locale>
#include <sstream>

int main() {
    // Input String
    char valueAsCString[] = "$567,789,675.89";

    // Put it in an istringstream for formatted extraction
    std::istringstream iss{ valueAsCString };

    // Assume US currency format (On Posix machines, please use "en-US") and set it for the stream
    std::locale myLocale("en_US");
    iss.imbue(myLocale);
    
    // Assume that everthing is good
    std::ios_base::iostate ioError{ std::ios_base::goodbit };

    // Here we will get the value in UNITS, so without a fraction!!!
    long double value{};

    // Parse the money string and get the result
    std::use_facet<std::money_get<char>>(myLocale).get(std::istreambuf_iterator<char>(iss), {}, false, iss, ioError, value);

    // Check Error state
    iss.setstate(ioError);
    if (iss)
        // Show result
        std::cout << std::setprecision(std::numeric_limits<long double>::digits10 + 1) << std::setw(25) << value / 100 << '\n';
    else
        std::cerr << "\nError during conversion\n";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM