-4

I'm trying to convert an amount from numbers to string. While converting 3070 to three thousand seventy only I noticed a flaw in the code, the output is supposed to be three thousand seventy only but instead of this the output is Three Thousand Rupees only.

I got the code from the internet,

When i debug the code, I see the following lines

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

The problem arises in this code because 1010,1020,.....,3070,3080,3090,4010,4020.etc all the numbers are % to 1000, that means if I enter these number the output will be wrong,

I am unable to get the proper logic here. I think i need to validate the rupees again inside another if condition.

Code below X Thousands

 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;
    }
glennsl
  • 28,186
  • 12
  • 57
  • 75
  • 1
    I think you need to add the following conversion as well. rupeestowords(rupees ) – Hasan Emrah Süngü Jan 24 '17 at 07:55
  • will you help me to resolve this?? can you show me the answer –  Jan 24 '17 at 07:56
  • 1
    Possible duplicate of [How can I convert an integer into its verbal representation?](http://stackoverflow.com/questions/554314/how-can-i-convert-an-integer-into-its-verbal-representation) – Pranav Patel Jan 24 '17 at 07:57
  • 1
    This can't be the whole code... The error must be in the brachen if((rupees / 100) > 0) or more likely in if ((rupees /10) > 0) – SirBirne Jan 24 '17 at 07:57
  • You've posted the code that does the x Thousand bit which is working fine. Post the code that does the tens (seventy) bit, or in fact just post all of it. – Tim Rutter Jan 24 '17 at 07:59

1 Answers1

1

In this code:

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

This line is wrong:

res = rupees % 100;   

Should be

res = rupees / 10;

Also the following line is wrong:

if ((rupees % 10) > 0)

Should be:

if ((rupees / 10) > 0)

Leaving:

if ((rupees / 10) > 0)
{
    res = rupees % 10;
    result = result + " " + rupeestowords(res);
}
Tim Rutter
  • 4,549
  • 3
  • 23
  • 47