Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four)

后端 未结 8 553
名媛妹妹
名媛妹妹 2021-02-01 11:37

Write C/C++/Java code to convert given number into words.

eg:- Input: 1234

Output: One thousand two hundred thirty-four.

Input: 10

Output: Ten

相关标签:
8条回答
  • 2021-02-01 12:13

    if you are interested in a ready solution then you may look at HumanizerCpp library (https://github.com/trodevel/HumanizerCpp) - it is a port of C# Humanizer library and it does exactly what you want.

    It can even convert to ordinals and currently supports 3 languages: English, German and Russian.

    Example:

    const INumberToWordsConverter * e = Configurator::GetNumberToWordsConverter( "en" );
    
    std::cout << e->Convert( 123 ) << std::endl;
    std::cout << e->Convert( 1234 ) << std::endl;
    std::cout << e->Convert( 12345 ) << std::endl;
    std::cout << e->Convert( 123456 ) << std::endl;
    
    std::cout << std::endl;
    std::cout << e->ConvertToOrdinal( 1001 ) << std::endl;
    std::cout << e->ConvertToOrdinal( 1021 ) << std::endl;
    
    
    const INumberToWordsConverter * g = Configurator::GetNumberToWordsConverter( "de" );
    
    std::cout << std::endl;
    std::cout << g->Convert( 123456 ) << std::endl;
    
    const INumberToWordsConverter * r = Configurator::GetNumberToWordsConverter( "ru" );
    
    std::cout << r->ConvertToOrdinal( 1112 ) << std::endl;
    

    Output:

    one hundred and twenty-three
    one thousand two hundred and thirty-four
    twelve thousand three hundred and forty-five
    one hundred and twenty-three thousand four hundred and fifty-six
    
    thousand and first
    thousand and twenty-first
    
    einhundertdreiundzwanzigtausendvierhundertsechsundfünfzig
    одна тысяча сто двенадцатый
    

    In any case you may take a look at the source code and reuse in your project or try to understand the logic. It is written in pure C++ without external libraries.

    Regards, Serge

    0 讨论(0)
  • 2021-02-01 12:14
    /*Maximum value that can be entered is 2,147,483,647
     * Program to convert entered number into string
     * */
    import java.util.Scanner;
    
    public class NumberToWords 
    {
    
    public static void main(String[] args) 
    {
        double num;//for taking input number
        Scanner obj=new Scanner(System.in);
        do
        {
            System.out.println("\n\nEnter the Number (Maximum value that can be entered is 2,147,483,647)");
            num=obj.nextDouble();
            if(num<=2147483647)//checking if entered number exceeds maximum integer value
            {
                int number=(int)num;//type casting double number to integer number
                splitNumber(number);//calling splitNumber-it will split complete number in pairs of 3 digits
            }
            else
                System.out.println("Enter smaller value");//asking user to enter a smaller value compared to 2,147,483,647
        }while(num>2147483647);
    }
    //function to split complete number into pair of 3 digits each
    public static void splitNumber(int number)
    {   //splitNumber array-contains the numbers in pair of 3 digits
        int splitNumber[]=new int[4],temp=number,i=0,index;
        //splitting number into pair of 3
        if(temp==0)
            System.out.println("zero");
        while(temp!=0)
        {
            splitNumber[i++]=temp%1000;
            temp/=1000;
        }
        //passing each pair of 3 digits to another function
        for(int j=i-1;j>-1;j--)
        {   //toWords function will split pair of 3 digits to separate digits
            if(splitNumber[j]!=0)
                {toWords(splitNumber[j]);
            if(j==3)//if the number contained more than 9 digits
                System.out.print("billion,");
            else if(j==2)//if the number contained more than 6 digits & less than 10 digits
                System.out.print("million,");
            else if(j==1)
                System.out.print("thousand,");//if the number contained more than 3 digits & less than 7 digits
                }
                }       
    }
    //function that splits number into individual digits
    public static void toWords(int number)
        //splitSmallNumber array contains individual digits of number passed to this function
    {   int splitSmallNumber[]=new int[3],i=0,j;
        int temp=number;//making temporary copy of the number
        //logic to split number into its constituent digits
    
        while(temp!=0)
        {
            splitSmallNumber[i++]=temp%10;
            temp/=10;
        }
        //printing words for each digit
        for(j=i-1;j>-1;j--)
          //{   if the digit is greater than zero
            if(splitSmallNumber[j]>=0)
                //if the digit is at 3rd place or if digit is at (1st place with digit at 2nd place not equal to zero)
            {   if(j==2||(j==0 && (splitSmallNumber[1]!=1)))
                {
                    switch(splitSmallNumber[j])
                    {
    
                        case 1:System.out.print("one ");break;
                        case 2:System.out.print("two ");break;
                        case 3:System.out.print("three ");break;
                        case 4:System.out.print("four ");break;
                        case 5:System.out.print("five ");break;
                        case 6:System.out.print("six ");break;
                        case 7:System.out.print("seven ");break;
                        case 8:System.out.print("eight ");break;
                        case 9:System.out.print("nine ");break;
    
                }
    
            }
            //if digit is at 2nd place
            if(j==1)
            {       //if digit at 2nd place is 0 or 1
                    if(((splitSmallNumber[j]==0)||(splitSmallNumber[j]==1))&& splitSmallNumber[2]!=0 )
                    System.out.print("hundred ");
                switch(splitSmallNumber[1])
                {   case 1://if digit at 2nd place is 1 example-213
                            switch(splitSmallNumber[0])
                            {
                            case 1:System.out.print("eleven ");break;
                            case 2:System.out.print("twelve ");break;
                            case 3:System.out.print("thirteen ");break;
                            case 4:System.out.print("fourteen ");break;
                            case 5:System.out.print("fifteen ");break;
                            case 6:System.out.print("sixteen ");break;
                            case 7:System.out.print("seventeen ");break;
                            case 8:System.out.print("eighteen ");break;
                            case 9:System.out.print("nineteen ");break;
                            case 0:System.out.print("ten ");break;
                            }break;
                            //if digit at 2nd place is not 1
                        case 2:System.out.print("twenty ");break;
                        case 3:System.out.print("thirty ");break;
                        case 4:System.out.print("forty ");break;
                        case 5:System.out.print("fifty ");break;
                        case 6:System.out.print("sixty ");break;
                        case 7:System.out.print("seventy ");break;
                        case 8:System.out.print("eighty ");break;
                        case 9:System.out.print("ninety ");break;
                        //case 0:   System.out.println("hundred ");break;
    
                }                           
            }           
        }
      }
    
    }
    
    0 讨论(0)
提交回复
热议问题