Convert number to letter

Hello everyone, I am a newbie in programming and I wanted to create an application that converts numbers to letters! I need help, thank you

This sounds like some homework task...
What are the rules to convert a number to a Letter?
What did you try?

A very good way to learn App Inventor is to read the free Inventor's Manual here in the AI2 free online eBook App Inventor 2 Book: Create Your Own Android Apps ... the links are at the bottom of the Web page. The book 'teaches' users how to program with AI2 blocks.
There is a free programming course here Course In A Box and the aia files for the projects in the book are here: App Inventor 2 Book: Create Your Own Android Apps
How to do a lot of basic things with App Inventor are described here: How do you...? .

Also do the tutorials Tutorials for MIT App Inventor to learn the basics of App Inventor, then try something and follow the Top 5 Tips: How to learn App Inventor

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

1 Like
  • Print the numbers on paper
  • fold the paper and put it in an envelope
  • Address the envelope
  • put a stamp on the envelope
  • seal the envelope
  • mail the envelope to send the letter
2 Likes

Hello Black Ghost

Perhaps you need to use the ASCII code for Letters (and numbers)?

1 Like

@BLACK_GHOST

Just use :

OnButton1.Click{
  Set(Textbox2.Text) = TextBox1.Text
}

And you are done with your Project.

Please mark as Solution if this was the one. :slightly_smiling_face:

I'm imagining that Button1 is your Convertir Button.

1 Like

Isn't that just copying from one box to the other without conversion?

@ChrisWard

But the TextBox actually itself converts numbers and characters into text.

He just wanted input in numbers from TextBox1 and output in Text in TextBox2, so that's what exactly happens in what I said.

How will that help? To the user, a number and a string are no difference. The OP probably wants to convert numbers to English numerals, e.g. one, two, three, or encode them to numbers.

OK then.

I'm making an aia for him.

Please wait.

If you want help, you need to be clear about what you mean by convert number to letter.

You might provide an example about what you would like to do.

3 Likes

If you want to make the numbers like -
for number - 10
return - one zero

then its easy (that's already done by me), else if you want returns like 10 it would be quite complicated.

I think I can make it though, but you may not be able to understand because you said :

1 Like

I'm working on

Option 2

Example :
On an input of the number : 2386
Output would be : two thousand three hundred eighty six

I'm not close to success though, but thinking of things.

If you are allowed to use an extension then this extension can be useful for you.
(I think this extension works offline too)

1 Like

@oseamiya Thanks

But I'm planning to do it all by myself.

Cuz, that has allowed me do things that are not present in MIT AI2 by default. These are important procedures (methods) such as,

String s = "Hello World!"

// (1)
s.IndexOf('e');
// (2)
boolean b1 = s.matches("Hello World!"); // true
boolean b2 = s.matches("Hi"); //false

And some more useful operations.

According to me this will help many users.

So I'm gonna do this project all by myself.

I'm first doing this in Java.
Next, I'll copy the whole thing in the form of MIT App Inventor 2 Blocks.

um...

image

3 Likes

@TIMAI2

I said String.indexOf();
The method which returns an int value.

or rather you may think it as String.charAt(); method.

Also, I said String.matches(String);

Which returns true if both the Strings are same.

How could you find a String.contains(String); method in my post ? :thinking:

So, you can can find java codes if you search on google and you can try creating it through blocks.

3 Likes

Here is also a Stack Overflow answer. (also with numbers to :fr: French words)

1 Like

Thanks @oseamiya and @Gordon_Lu

But, I didn't see that before actually. :sweat_smile:

Had to do it all by myself.

At last the java code is done.

Its 1:57 AM here and I think now its time to get to sleep after this success.

BTW, my program can convert any long value to words, both in negative and positive (not decimals).

Also, BTW, long values go till pentillions.

Conversion

(1 pentillion = 1000000 trillion)

I took me quite a lot of thinking and two days to make it in java.

And I know putting it in MIT AI2 would be a hard task too.

But I'm gonna do it. (Obviously after some sleep, cuz I mentioned the present time.)

Bye. :wave:

Here is the Java Program :

package Practice.Program;

public class Num2Words {
  
  private static long number;
  private static String words = new String("");
  
  public Num2Words(long n){ number = n; }
  
  static String[] tp = {"", "thousand ", "million ", "billion ", "trillion ", "quadrillion ", "pentillion "};
  static String[] od = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
  static String[] td = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
  static String[] tn = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
  
  public String toWords(){
    words = Long.toString(number);
    int l = words.length();
    boolean n = false;
    
    if (number < 0){ n = true; number *= -1; --l;}
    
    breaker : for (byte LOL = 0; LOL < 1; LOL++){
      
      if (StringHasChar(words, '.')){ words = "Decimals not supported."; break breaker;}
      if (l>19){ words = "too big"; break breaker;}
      
      if (l==1)      words = oneDigit(number);                    // one digit
      
      else if (l==2) words = twoDigits(number);                   // two digits 
      
      else if (l==3) words = threeDigits(number);                 // three digits
      
      else words = nDigits(number);                               // multiple digits
      
    }
    
    if (n == true) {words = "Minus "+words; number *= -1;}
   return words;
  }
  
  private static String oneDigit(long n){
    char[] ns = Long.toString(n).toCharArray();
    return od[(char2num(ns[0]))];
  }
  
  private static String twoDigits(long n){
    String s = "";
    char[] ns = Long.toString(n).toCharArray();
    if (n < 20){
      s = td[(char2num(ns[1]))];
    } else if (n < 100){
        s = tn[char2num(ns[0])];
        if (char2num(ns[1])!=0){ s += " " + od[char2num(ns[1])]; }
      }
    return s;
  }
  
  private static String threeDigits(long n){
    String s = "", is = Long.toString(n);
    long[] ns = String2longArray(is);
    
    s = od[(int)(ns[0])] + " hundred";
    if (ns[1]!=0&&ns[2]!=0){
      long i = (ns[1])*10+(ns[2]);
      s += " " + twoDigits(i);
    } else {
        if (ns[1]!=0) s += " " + twoDigits((ns[1]*10)); 
        if (ns[2]!=0) s += " " + oneDigit(ns[2]);
      }
    
    return s;
  }
  
  private static String nDigits(long n){
    String s = threeDigits(n);
    int tpi = ((digits(n)+2)/3)-1, rtpi = 0;
    long[] tfr = threeFromRight(n);
    s = "";
    
    for (; tpi >= 0; tpi--, rtpi++){
      s = new Num2Words(tfr[tpi]).toWords() + " " + tp[rtpi] + s;
    }
    
    return s;
  }
  
  private static int digits(long n){
    long ans = 0;
    for (; n>0; n/=10, ans++);
    return (int)(ans);
  }
  
  private static long[] threeFromRight(long n){
    int tpi = (digits(n)+2)/3, nl = digits(n);
    long[] ans=new long[tpi], ia=String2longArray(Long.toString(n)), ta=new long[3];
    
    for (int i=nl, t=2, c=0, l=tpi*3; l >= 0; l--, c++, i--, t--){
      try {ta[t] = ia[i-1];}
      catch (ArrayIndexOutOfBoundsException e){ta[t]=0;}
      if (((c+1)%3)==0){ 
        --tpi;
        ans[tpi] = longArrays2long(ta);
        
        t = 3;
        c = -1;
      }
    }
    
    return ans;
  }
  
  private static int char2num(char c){
    char[] ca = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    byte ia[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},  ans = -1;
    
    for (byte i = 0; i < 10; i++){ if (c == ca[i]){ ans = ia[i]; break;} }
    return ans;
  }
  
  private static long[] String2longArray(String s){
    char[] ca = s.toCharArray();
    byte l = (byte)s.length();
    long[] ans = new long[l];
    
    for (byte i = 0; i < l; i++){ ans[i] = char2num(ca[i]); }
    return ans;
  }
  
  private static long longArrays2long(long[] ia){
    long ans = 0;
    try { for (int i = 0; i >= 0; ans += ia[i], ans *= 10, ++i); } 
    catch (ArrayIndexOutOfBoundsException e){}
    ans/=10;
    
    return ans;
  }
  
  private static boolean StringHasChar(String s, char c){
    char[] sca = s.toCharArray();
    byte l = (byte)(s.length());
    boolean ans = false;
    for (byte i = 0; i > l; i++){
      if (sca[i] == c) ans = true;
    }
    return ans;
  }
  
  public static void main(String[] args){
    long n = -1053664648543756767l;
    String words = new Num2Words(n).toWords();
    
    System.out.println(n + " : " + words);
  }
  
}

I've also posted my program as an answer to a question in Stack Overflow.


Would anyone like to help me out with putting some procedures into MIT AI2 ?

If Yes

How you can help me :-

  1. Make an aia.
  2. Make the procedures.
  3. Send the aia.

Helping me is fully your own wish. I'd not force anyone.

Thank You.