Convert Chinese numbers to Arabic numbers

Source: Internet
Author: User

Package cn.edu. bjfu. test;

Public class Divert {

/**
* It only supports trillion levels and is absolutely able to meet daily needs.
*
* @ Param src
* @ Return
*/
Public static double divert (String src ){
String temp = src. replaceAll ("whole", ""). split ("circle", 2) [0];
Int index1 = src. indexOf (" ");
Int index2 = src. indexOf ("points ");
String [] srcs1 = temp. split (" ");
Long ret = 0;
If (srcs1.length = 1 ){
If (temp. contains (" ")){
Ret = fromDBK2Long (temp) * 100000000;
} Else {
Ret = fromDBK2Long (temp );
}
} Else if (srcs1.length = 2 ){
Ret = fromDBK2Long (srcs1 [0]) * 100000000 + fromDBK2Long (srcs1 [1]);
}


StringBuilder sb = new StringBuilder ();
Sb. append (ret );
If (index1>-1)
Sb. append (".")
. Append (Divert. getInt (String
. ValueOf (src. charAt (index1-1 ))));
If (index2>-1 ){
If (index1>-1)
Sb. append (Divert. getInt (String
. ValueOf (src. charAt (index2-1 ))));
Else
Sb. append (".")
. Append (0)
. Append (Divert. getInt (String
. ValueOf (src. charAt (index2-1 ))));
}

Return Double. parseDouble (sb. toString ());
}

Private static long fromDBK2Long (String src ){
String [] srcs = src. split ("Ten Thousand", 2 );
Long ret = 0;

If (srcs. length = 1 ){
If (src. contains ("Ten Thousand ")){
String [] temp = srcs [0]. split ("0 ");
For (String tmp: temp)
Ret + = Divert. doSomething (tmp );

Ret * = 10000;
} Else {
String [] temp = srcs [0]. split ("0 ");
For (String tmp: temp)
Ret + = Divert. doSomething (tmp );
}
} Else if (srcs. length = 2 ){
String [] temp1 = srcs [0]. split ("0 ");
String [] temp2 = srcs [1]. split ("0 ");
For (String tmp: temp1)
Ret + = Divert. doSomething (tmp );

Ret * = 10000;

For (String tmp: temp2)
Ret + = Divert. doSomething (tmp );

} Else {

}

Return ret;
}

Private static long doSomething (String src ){
If (src. length () = 1)
Return Divert. getInt (src );
Else if (src. length () = 2 ){
Return Divert. getInt (String. valueOf (src
. CharAt (0 )))
* Divert. getInt (String. valueOf (src
. CharAt (1 )));
} Else if (src. length ()> 2 ){
If (Divert. getInt (String. valueOf (src
. CharAt (2) <10000 ){

Return Divert
. DoSomething (src. substring (

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.