该解决方案最多支持 66 位数字,并且可以根据不同的计数系统进行修改。要了解此计数系统,请参阅https://ptdeepali.wordpress.com/2013/10/28/vedic-numbering-system/。
package com.number_to_word;
import java.util.Scanner;
/**
* This program is to get the word value of given number.
* <p>Number can be ranges from 0 to 66th power of 10 (66 digits).</p>
* Number can be passed through Scanner and it will print the word value of that number.
*
* @author DHARAMRAJSINGH
*
*/
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter number: ");
String n = sc.nextLine();
System.out.println(getWord(n));
sc.close();
}
/**
* This method is using array inside it and this array is 2D array contains word values.
* <p>This array ranges from arr[0][0] to arr[9][9]. </p>
* <p>The array is initialized in a way that it will return values like, for arr[0][0] returns zero
* </br> arr[0][1] returns one, arr[2][1] returns twenty one, arr[9][5] returns ninety five, and so on. </p>
* The logic is the number <strong>n</strong> is passed into it and divided by 10, the quotient will be index0 and remainder will be index1,
* and return arr[index0][index1].
* </br>
* @param n will be in the range of 0 to 99
* @return the word value ranges from 0 to 99
*/
private static String getValue(int n) {
int index0 = 0, index1 = 0;
if(n < 0 || n > 99) {
throw new IllegalArgumentException("number cannot be less than zero or greater than 99");
}
if(n < 10) {
index1 = n;
} else {
index1 = n%10;
index0 = n/10;
}
String arr [][] = {
// for "zero", "one" .... "nine"
{"शुन्य", "एकः", "द्वौ", "त्रयः", "चत्वारः", "पञ्च", "षट्", "सप्त", "अष्ट", "नव"},
// for "ten", "eleven" .... "nineteen"
{"दश", "एकादशम्", "द्वादशम्", "त्रयोदशम्", "चतुर्दशम्", "पञ्चदशम्", "षोडशम्", "सप्तदशम्", "अष्टादशम्", "नवदशम्"},
// for "twenty", "twenty one" .... "twenty nine"
{"विंशति", "एकाविंशति", "द्वाविंशति", "त्रयोविंशति", "चतुर्विंशति", "पञ्चविंशति", "षड्विंशति", "सप्तविंशति", "अष्टाविंशति", "नवविंशति"},
// for "thirty", "twenty one" .... "thirty nine"
{"त्रिंशत्", "एकत्रिंशत्", "द्वात्रिंशत्", "त्रयत्रिंशत्", "चतुस्त्रिंशत्", "पञ्चत्रिंशत्", "षट्त्रिंशत्", "सप्तत्रिंशत्", "अष्टात्रिंशत्", "एकोनचत्वारिंशत्"},
{"चत्वारिंशत्", "एकचत्वारिंशत्", "द्विचत्वारिंशत्", "त्रिचत्वारिंशत्", "चतुश्चत्वारिंशत्", "पञ्चचत्वारिंशत्", "षट्चत्वारिंशत्", "सप्तचत्वारिंशत्", "अष्टचत्वारिंशत्", "एकोनपञ्चाशत्"},
{"पञ्चाशत्", "एकपञ्चाशत्", "द्विपञ्चाशत्", "त्रिपञ्चाशत्", "चतुःपञ्चाशत्", "पञ्चपञ्चाशत्", "षट्पञ्चाशत्", "सप्तपञ्चाशत्", "अष्टपञ्चाशत्","एकोनषष्ठिः"},
{"षष्ठिः", "एकषष्ठिः", "द्विषष्ठिः", "त्रिषष्ठिः", "चतुःषष्ठिः", "पञ्चषष्ठिः", "षट्षष्ठिः", "सप्तषष्ठिः", "अष्टषष्ठिः", "एकोनसप्ततिः"},
{"सप्ततिः", "एकसप्ततिः", "द्विसप्ततिः", "त्रिसप्ततिः", "चतुःसप्ततिः", "पञ्चसप्ततिः", "षट्सप्ततिः", "सप्तसप्ततिः","अष्टसप्ततिः","एकोनाशीतिः"},
{"अशीतिः", "एकाशीतिः", "द्वशीतिः", "त्र्यशीतिः", "चतुरशीतिः", "पञ्चाशीतिः", "षडशीतिः", "सप्ताशीतिः", "अष्टाशीतिः","एकोननवतिः"},
// for "ninety", "two" .... "ninety nine"
{"नवतिः", "एकनवतिः", "द्विनवतिः", "त्रिनवतिः", "चतुर्नवतिः", "पञ्चनवतिः", "षण्णवतिः", "सप्तनवतिः", "अष्टनवतिः","एकोनशतम्"}
};
return arr[index0][index1];
}
/**
* This method returns the place value of the digit, like for index value 2 it will return hundred,
* for index value 3 and 4 it will thousand, for index value 5 and 6 it will lacs, and so on.
* <p>It return empty string for index 0 and 1 as it does not have specific name for that.
* @param n is index value of digit
* @return place value of index (position like unit place, tens place etc.)
*/
private static String getPlaceValue(int n) {
String [] arr = {"", "", "शत", "सहस्त्र", "सहस्त्र", "लक्ष", "लक्ष", // this line is same as "", "", "hundred", "thousand", "thousand", "lac", "lac"
"कोटि", "कोटि", "कोटि", "कोटि", "कोटि", // same as "crore" , "crore" , "crore" and so on
"शङ्कु", "शङ्कु", "शङ्कु", "शङ्कु", "शङ्कु",
"महाशङ्कु", "महाशङ्कु", "महाशङ्कु", "महाशङ्कु", "महाशङ्कु",
"व्ऋंद", "व्ऋंद", "व्ऋंद", "व्ऋंद", "व्ऋंद",
"महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद", "महाव्ऋंद",
"पद्म", "पद्म", "पद्म", "पद्म", "पद्म",
"महापद्म", "महापद्म", "महापद्म", "महापद्म", "महापद्म",
"खर्व", "खर्व", "खर्व", "खर्व", "खर्व",
"महाखर्व", "महाखर्व", "महाखर्व", "महाखर्व", "महाखर्व",
"समुद्र", "समुद्र", "समुद्र", "समुद्र", "समुद्र",
"ओघ", "ओघ", "ओघ", "ओघ", "ओघ",
"महोघ", "महोघ", "महोघ", "महोघ", "महोघ"
};
if(n >= arr.length) {
throw new IllegalArgumentException("Not in range");
} else {
return arr[n];
}
}
/**
* This method takes index(position of digit) as the input and return the number of digits need to be taken in consideration of calculation.
* <p> Like for index 0 only one digit will be passed to get it's word value, for index 1 two digits(00 to 99) need to be passed,
* for index 2 inly one need to be passed and so on.
* @param index position of digit
* @return next index value
*/
private static int numOfDigitsToInclude(int index) {
int [] a = {1, 2, 1, 1, 2, 1, 2,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5};
return a[index];
}
/**
* This method returns next index value for calculations need to be performed.
* @param index
* @return
*/
private static int nextIndex ( int index) {
int [] a = {-1, -1, 1, 2, 2, 4, 4, 6, 6, 6, 6, 6, 11, 11, 11, 11, 11,
16, 16, 16, 16, 16,
21, 21, 21, 21, 21,
26, 26, 26, 26, 26,
31, 31, 31, 31, 31,
36, 36, 36, 36, 36,
41, 41, 41, 41, 41,
46, 46, 46, 46, 46,
51, 51, 51, 51, 51,
56, 56, 56, 56, 56,
61, 61, 61, 61, 61
};
return a[index];
}
/**
* This method takes number as input in String(as it exceeds the limit for long) form.
* <p> Convert into array and loop over it to get word value for number.
* @param num
* @return
*/
private static String getWord(String num) {
if(num.length() == 0) {
throw new IllegalArgumentException("Not valid");
}
if(num.equals("0")){
return getValue(0);
}
String s = "" + num;
int size = s.length();
long [] a = new long [size];
String n = num; int k=0;
for(int j = num.length() -1 ; j >= 0; j--) {
a[k++] = Integer.parseInt(""+ num.charAt(j) );
}
int i = a.length-1;
int arg = 0;
int include = 0;
StringBuffer buffer = new StringBuffer();
while(i >= 0) {
arg = 0;
include = numOfDigitsToInclude(i);
if(include == 2) {
arg = (int) ((int) 10*a[i] + a[i-1]);
} else if(include == 1) {
arg = (int) a[i];
} else {
for(int l = 1; l<=include; l++) {
arg = (int) (10*arg + a[i+1-l]);
}
//recursion used for arg value more than 99
buffer.append(getWord(""+ arg)).append(" ");
}
if(arg == 0) {
i = nextIndex(i);
if(i <= 0) {
continue;
}
continue;
}
if(include <= 2) {
buffer.append(getValue(arg)).append( " ");
}
buffer.append(getPlaceValue(i)).append(" ");
i = nextIndex(i);
}
return buffer.toString();
}
}