|
I also wrote one. I thought that there were too many digits and it didn’t make sense, so I did 15 digits.
Another: I googled a bit, some said one trillion = one trillion, and some said one trillion = 110 billion, I temporarily think that one trillion = one trillion. The code is as follows: Please correct me if there is something wrong!
package numberToCn;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
public class NumberToCnModify {
List cnList = null, danweiList = null;
StringBuffer cnStr = null;
char[] tempStr = null;
int charLength = 0;
The
public NumberToCnModify(){
cnList = new ArrayList();
danweiList = new ArrayList();
cnStr = new StringBuffer();
}
public void init(){
cnList.add("zero");
cnList.add("一");
cnList.add("Two");
cnList.add("Three");
cnList.add("Ran");
cnList.add("Wu");
cnList.add("Lu");
cnList.add("柒");
cnList.add("捌");
cnList.add("Jiu");
The
danweiList.add("");
danweiList.add("Pick up");
danweiList.add("百");
danweiList.add("仟");
danweiList.add("10,000");
danweiList.add("100 million");
danweiList.add("Mega");
}
public boolean isNumber(char tempChar){
boolean isNum = true;
if((tempChar<'0')||(tempChar>'9')){
System.out.println();
System.out.println("Not Number"+tempChar);
isNum=false;
}
return isNum;
}
public void getInputString(String inputStr){
tempStr = inputStr.toCharArray();
charLength = tempStr.length;
if(charLength>15){
System.out.println("The Number You Input Is Too Long!");
System.exit(0);
}
}
public String convertToBaseCn(int startNum,int endNum,String danwei){
int allZero = 0;
int tempNum = 0;
String tempString = "";
for(int i=startNum;i<endNum;i++){
tempNum = Integer.parseInt(String.valueOf(tempStr[i]));
if(i==startNum&&tempNum==0||i==startNum+1&&tempNum==0&&Integer.parseInt(String.valueOf(tempStr[i-1]))==0||i==startNum+2&&tempNum==0&&Integer. parseInt(String.valueOf(tempStr[i-1]))==0&&Integer.parseInt(String.valueOf(tempStr[i-2]))==0||i==startNum+3&&tempNum==0&&Integer.parseInt(String .valueOf(tempStr[i-1]))==0&&Integer.parseInt(String.valueOf(tempStr[i-2]))==0&&Integer.parseInt(String.valueOf(tempStr[i-3]))==0 ){
if(i==startNum+3&&tempNum==0&&Integer.parseInt(String.valueOf(tempStr[i-1]))==0&&Integer.parseInt(String.valueOf(tempStr[i-2]))==0&&Integer.parseInt( String.valueOf(tempStr[i-3]))==0){
allZero = 1;
}
//If the first one is zero or 1, 2 both are 0 or 1, 2, 3, 1, 2, 3, 4 are 0, there is no need to print zero and corresponding units.
}else{
if(i==endNum-1&&tempNum==0||i==endNum-2&&tempNum==0&&Integer.parseInt(String.valueOf(tempStr[i+1]))==0||i==endNum-3&&tempNum== 0&&Integer.parseInt(String.valueOf(tempStr[i+1]))==0){
//If the last one is 0, and one and the following are all 0, there is no need to print 0
}else{
tempString+=cnList.get(tempNum).toString();
}
The
if(tempNum!=0){//If it is 0, there is no need to print the unit
tempString+=danweiList.get(endNum-(i+1)).toString();
}
The
}
}
The
if(allZero == 0){
tempString+=danwei;
}
The
return tempString;
}
public String convertToCn(){
int notNumber = 0;
int zeroRecord = 0;//When two or more bit segments are 0 at the same time, as long as a 0 is displayed
String tempString = "";
The
try{
The
for(int i=0;i<charLength;i++){
if(!isNumber(tempStr[i])){
notNumber = 1;
break;
}
}
The
if(notNumber == 1){
System.out.println("Please Input Number!");
return "";
}else{
if(charLength>12){
tempString = convertToBaseCn(0,charLength-12,"Mega");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString = convertToBaseCn(charLength-12, charLength-8, "100 million");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString = convertToBaseCn(charLength-8, charLength-4, "10,000");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString=convertToBaseCn(charLength-4,charLength,"");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
}else if(charLength>8){
tempString=convertToBaseCn(0,charLength-8,"100 million");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString=convertToBaseCn(charLength-8,charLength-4,"10,000");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString=convertToBaseCn(charLength-4,charLength,"");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
}else if(charLength>4){
tempString=convertToBaseCn(0,charLength-4,"10,000");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
The
tempString=convertToBaseCn(charLength-4,charLength,"");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
}else{
tempString=convertToBaseCn(0,charLength,"");
if(tempString.equals("")){
zeroRecord ++;
}else{
zeroRecord = 0;
}
cnStr.append(tempString);
if(zeroRecord ==1 ){
cnStr.append("zero");
}
}
}
}catch(NullPointerException ne){
System.out.println("NullPointerException:"+ne.getMessage());
}catch(IndexOutOfBoundsException iobe){
System.out.println("IndexOutOfBoundsException:"+iobe.getMessage());
}finally{
The
}
return cnStr.toString();
}
public static void main(String[] args){
NumberToCnModify ntc = new NumberToCnModify();
ntc.init();
ntc.getInputString("300340000000130");
System.out.println(ntc.convertToCn());
}
} |
|