Numeral to Alphabetical

Discussion in 'C' started by usama_muneeb, Jun 19, 2008.

  1. usama_muneeb

    usama_muneeb Banned

    Mar 7, 2008
    Likes Received:
    Trophy Points:
    Hi all,

    I am required to write a program in an exercise of a book to transliterate a number into English. I can do it into five languages (Arabic, Persian, Urdu, English, Punjabi) but when it comes to writing a program, its tedious. I can't make out the strategy. For being a programmer, you need to write the strategy (what variables, functions, you'll be having) and that alone is a difficult task.

    I thought I should convert it into a float (like a base in standard notation in maths). Like 9 38 to be 9.38 (leave x 10^2). Then using an char array storing the digit names, get 0 to be zero. The sequence would be to convert the float into int (temporarily (so only the first digit remains)). Then subtract 9 (1st digit) and multiply by ten. Do the same with 3 (that now stands at position 3.8). Do this until number % 10 != 0. But how will float store large values?


    unsigned float (is it any kind)?
  2. xpi0t0s

    xpi0t0s Mentor

    Aug 6, 2004
    Likes Received:
    Trophy Points:
    Senior Support Engineer
    I don't see how converting the numbers to floats helps, especially as floats are less accurate than ints and you risk losing data. You need to parse the number from left to right, you can do that by comparing it with 10^n (int mask=1; while (mask<num) num*=10; then you know that mask is the first power of ten higher than num), or you can sprintf the number to a string and use strlen to determine the order of magnitude of each digit. Or you can divide the number by certain powers of 10 to determine how many of that group there are (e.g. 15000 divide by 1000 to determine how many thousands: 15 (once you've removed millions and above)).

    The main difficulty I've found when trying to do this is how to put the complete number together. Here are some examples:
    101: One hundred and one
    1101: One thousand one hundred and one
    1001: One thousand and one
    10050: Ten thousand and fifty
    10150: Ten thousand one hundred and fifty
    110150: One hundred and ten thousand one hundred and fifty

    Those ands look almost random! Is there an and after "ten thousand" or not??? Why "one hundred and fifty", but not "fifty and three"? What's with four and twenty for 24?
    A simple program might phrase these without ands, for example "One hundred ten thousand one hundred fifty" but that doesn't read right and might be misinterpreted as a list of three numbers: 100, 10100, 50.

    But you're right, this is tedious and some of the rules only apply if there are numbers of lower magnitude, c.f. 1000, 1001, 1101, after determining the first digit is "one thousand" you can't just stick down "and" without seeing what comes next and how large it is.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice