将货币值从字符串转换为long double [英] converting a monetary value from string to long double

查看:94
本文介绍了将货币值从字符串转换为long double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序输入货币量并输出,其前面带有美元符号,然后用逗号将其分成三位数组成的组
我将把钱作为字符串输入,并调用名为mstold()的函数,并返回一个等同于long double的数字,所以我的问题是如何获取一个等于string的double double的数字,这是我的代码::)

I''m writing a program that inputs the amount of money and outputs it preceded by dollar sign and divided by commas into groups of three digits
I''ll enter the money as string and call function called mstold()and returns an equivalent number as long double so my problem is how can I get the a number long double equivalent to string and this is my code : :)

long double mstold(string money)
{
    long double amnt;
    for(int i=0;i<money.length();i++)
    {
        amnt=money[i]*10+1;
    }
    return amnt;
}
int _tmain(int argc, _TCHAR* argv[])
{
    string money;
    cout<<"Enter the amount of money you want to convert to long double";
    cin>>money;
    cout<<"\n\namount in long double :  "<<mstold(money)<<endl;
return 0;
}


谢谢.

[更新]
非常感谢OriginalGriff和Alain Rist回答了我的问题,您的回答确实很有帮助.
[/update]


Thank You .

[update]
Thanks a lot to OriginalGriff and Alain Rist for repling to my question your answers really help.
[/update]

推荐答案

您确实意识到字符串是字符序列,对吗?
为了将其转换为数字,是否需要正确处理诸如"123"之类的字符串?
您的代码
You do realise that a string is a sequence of characters, don''t you?
And that a string of characters such as "123" needs to be handled properly in order to convert it to a number?
Your code
for(int i=0;i<money.length();i++)
{
    amnt=money[i]*10+1;
}

查看字符串中的每个字符:"1",然后是"2",然后是"3".但是这些与数字1、2和3不同:它们是字符.这意味着它们具有一个字符值,该字符值将转换为显示设备的可见数字,而不是您看到的数字值. 此处 [ ^ ]是一个表格,显示了针对显示值的字符值系统.如果您尝试将它们视为您所做的数字,那么您的数字中会出现巨大的错误.

而是使用将字符串转换为数字的内置函数:atoi,atol,atod等.

Looks at each character in the string: ''1'' then ''2'' then ''3''. But these are not the same as the numbers 1, 2, and 3: they are characters. What that means is that they have a character value which is translated to a display device as a visible digit, not the numeric value that you see. Here[^] is a table showing a system of character values against display values. If you try to treat them as numbers they way you are doing, you will get huge errors in your numbers.

Instead, use the built in functions which convert strings to numbers: atoi, atol, atod etc.

amnt = atod(money);

代替您的for循环. (还有其他方法,但我会让他们留给您的老师!)

instead of your for loop. (There are other ways, but I''ll leave them for your tutor!)



标准C ++库提供了必要的工具集,虽然不是很漂亮,但是可以工作:
Hi,
The Standard C++ Library provides the necessary toolset, not very sexy, but works:
#include <locale>     // for locale, money_get, use_facet
#include <sstream>    // for istringstream, string
#include <iterator>   // for istreambuf_iterator
long double mstold(const std::string& amount)
{
    using std::ios;
    using std::use_facet;
    using std::money_get;
    using std::istreambuf_iterator;

    const std::locale loc("us");
    std::istringstream iss(amount);
    iss.imbue (loc);
    ios::iostate state = ios::goodbit;
    long double value = {0};

    use_facet<money_get<char> >(loc).get(
        iss, istreambuf_iterator<char>(), false, iss, state, value);

    if ((state & ios::failbit) == ios::failbit)
    {
        // Handle error
    }

    return value / 100;
}


[针对代码样式和错误处理进行了编辑]
欢呼声,
AR



cheers,
AR


这篇关于将货币值从字符串转换为long double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆