Java Arabic numeral to Chinese expression

Keywords: less Java

In the interview, the algorithm problem is to translate Arabic numerals into Chinese characters. For example, 123 is 123, 10080 is 1080

I tried to implement it myself. I found that the implementation was too cumbersome, there were too many codes, and there were places I couldn't think about easily. I posted my own writing method and the relatively simple one I found on the Internet

My own writing:

public class NumConvertUtil{
	private static final String[] nums=new String[]{"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
	private static final String[] units=new String[]{"Ten","hundred","thousand","ten thousand","Billion"};

    public static void main(String[] args){
        //Create an input object
		Scanner sc=new Scanner(System.in);
		//Get the string entered by the user
		String str=null;
		int times=0;
		while(times<20){
			times++;
			System.out.print("please enter a number:");
			str=sc.nextLine();
			int num=Integer.parseInt(str);
			String res=convert(num);
			System.out.print("The result is:"+res);
		}
		
		//for(int i=10001;i<12001;i++){
		//	System.out.println(convert(i));
		//}
    }

	/**
	* Converting numbers to Chinese characters, for example 123 123 123
	*   10340 ten thousand three hundred and forty
	*/
	public static String convert(int input){
		
		String ouput="";
		if (input>=100000000){
			ouput=thousandAndLess(ouput,input/100000000);
			ouput+=units[4];
		}
		
		input=input%100000000;
		if(input>=10000){
			ouput=thousandAndLess(ouput,input/10000);
			ouput+=units[3];
		}else{
			ouput+=addZero(ouput);
		}
		
		input=input%10000;
		ouput=thousandAndLess(ouput,input);
		return ouput;
	}

	//Get thousands or less
	private static String thousandAndLess(String ouput,int input){
		if (input/1000>0){
			ouput+=nums[input/1000];
			ouput+=units[2];
		}else{
			ouput+=addZero(ouput);
		}
		input=input%1000;
		if (input/100>0){
			ouput+=nums[input/100];
			ouput+=units[1];
		}else{
			ouput+=addZero(ouput);
		}
		input=input%100;
		if (input/10>0){
			if(ouput.length()>0||input/10>1){
				ouput+=nums[input/10];
			}
			ouput+=units[0];
		}else{
			ouput+=addZero(ouput);
		}
		input=input%10;
		if (input>0){
			ouput+=nums[input];
		}
		if (ouput.length()>0&&nums[0].equals(ouput.substring(ouput.length()-1))){
			ouput=ouput.substring(0,ouput.length()-1);
		}
		return ouput;
	}

	private static String addZero(String ouput){
		if(ouput.length()>0&&!nums[0].equals(ouput.substring(ouput.length()-1))&&!units[0].equals(ouput.substring(ouput.length()-1))){
			return nums[0];
		}else{
			return "";
		}
	}
}

Writing on the Internet
Converting numbers to Chinese characters in java

public class NumConvertChinese{
	private static final String[] nums=new String[]{"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
	private static final String[] units=new String[]{"","Ten","hundred","thousand","ten thousand","Ten","hundred","thousand","Billion","Ten","hundred","thousand"};

    public static void main(String[] args){
        //Create an input object
		Scanner sc=new Scanner(System.in);
		//Get the string entered by the user
		/* String str=null;
		int times=0;
		System.out.print("Please enter the number: ";
		str=sc.nextLine();
		int num=Integer.parseInt(str);
		String res=convert(num);
		System.out.print("The result is: "+ res"; */
		/* while(times<20){
			times++;
			System.out.print("Please enter the number: ";
			str=sc.nextLine();
			int num=Integer.parseInt(str);
			String res=convert(num);
			System.out.print("The result is: "+ res";
		} */
		
		for(int i=10001;i<12001;i++){
			System.out.println(convert(i));
		}
    }

	/**
	* Converting numbers to Chinese characters, for example 123 123 123
	*   10340 ten thousand three hundred and forty
	*/
	public static String convert(int input){
		
		String output="";
		int count=0;
		while(input>0){
			output=nums[input%10]+units[count]+output;
			input=input/10;
			count++;
		}

		return output.replaceAll("Zero[Hundred and ten]", "Zero").replaceAll("Zero+ten thousand", "ten thousand")
                .replaceAll("Zero+Billion", "Billion").replaceAll("Millions upon millions", "Billion")
                .replaceAll("Zero+", "Zero").replaceAll("Zero $", "");
	}
}

Posted by Edwin Okli on Mon, 02 Dec 2019 04:57:16 -0800