【问题标题】:issue with conversion of amount, from numbers to character金额转换问题,从数字到字符
【发布时间】:2017-01-24 07:51:32
【问题描述】:

我正在尝试将金额从数字转换为字符串。在将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;
    }

【问题讨论】:

  • 我认为您还需要添加以下转换。 rupeestowords(卢比)
  • 你能帮我解决这个问题吗?你能告诉我答案吗
  • 这不可能是整个代码...错误必须在brachen中 if((rupees / 100) > 0) 或更可能在 if ((rupees /10) > 0)
  • 您已经发布了运行正常的 x 千位代码。发布执行十(七十)位的代码,或者实际上只是发布所有代码。

标签: c# numbers string-formatting


【解决方案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);
}

【讨论】:

  • 仍然没有进入循环
  • 编辑我的答案,还有另一行不正确 - 你选择了一些错误的代码来使用
  • 现在只有当我的输入是 3050 时,我才能得到三千五卢比
  • 再次更改答案-您当然可以自己做应该是:res = rupees / 10;
猜你喜欢
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 2022-01-08
  • 2021-01-02
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多