转换金额,从数字到字符的问题

时间:2017-01-24 07:51:33

标签: c# numbers string-formatting

我正在尝试将数量从数字转换为字符串。在将3070转换为three thousand seventy only时,我发现代码中存在缺陷,输出应为three thousand seventy only,但输出为Three Thousand Rupees only

我从互联网上获取了代码,

当我调试代码时,我看到以下行

if ((rupees / 1000) > 0)
        {
            res = rupees / 1000;
            rupees = rupees % 1000;
            result = result + ' ' + rupeestowords(res) + " Thousand";
        }

此代码出现问题,因为1010,1020,.....,3070,3080,3090,4010,4020.etc所有数字都是%到1000,这意味着如果输入这些数字,输出将是错误的,

我无法在这里得到正确的逻辑。我想我需要在另一个if条件下再次验证卢比。

低于X千的代码

 if ((rupees / 100) > 0)
        {
            res = rupees / 100;
            rupees = rupees % 100;
            result = result + ' ' + rupeestowords(res) + " Hundred";
        }
        if ((rupees % 10) > 0)
        {
            res = rupees % 100;
            result = result + " " + rupeestowords(res);
        }
        result = result + ' ' + " Rupees only";
        return result;
    }

1 个答案:

答案 0 :(得分:1)

在此代码中:

if ((rupees % 10) > 0)
{
    res = rupees % 100;
    result = result + " " + rupeestowords(res);
}

这一行错了:

res = rupees % 100;   

应该是

res = rupees / 10;

以下行也是错误的:

if ((rupees % 10) > 0)

应该是:

if ((rupees / 10) > 0)

离开:

if ((rupees / 10) > 0)
{
    res = rupees % 10;
    result = result + " " + rupeestowords(res);
}