The original go language realizes the RMB small capitalization

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Use the Golang regular expression to implement the RMB lowercase to uppercase, the code is as follows:

Package Mainimport ("RegExp" "StrConv" "Log" "FMT") func convertnumtocny (num float64) string {strnum: = StrConv. Formatfloat (num*100, ' F ', 0, ") sliceunit: = []string{" thousand "," Bai "," pick "," billion "," Qian "," Bai "," pick "," million "," Thousand "," Bai "," pick "," Yuan "," angle "," min " }//log. Println (Sliceunit[:len (Sliceunit)-2]) S: = Sliceunit[len (Sliceunit)-len (strnum): Len (sliceunit)]upperdigitunit: = map[string]string{"0": "0", "1": "One", "2": "II", "3": "Three", "4": "The Restaurant", "5": "WU", "6": "Lu", "7": "Qi", "8": "Ba", "9": "Jiu"}STR: = "" For K, V: = Range strnum[:] {str = str + upperdigitunit[string (v)] + s[k]}reg, err: = RegExp. Compile (' Certer $ ') str = Reg. Replaceallstring (str, "whole") Reg, err = RegExp. Compile (' 0 pt. ') str = Reg. Replaceallstring (str, "0") Reg, err = RegExp. Compile (' 0 min $ ') str = Reg. Replaceallstring (str, "whole") Reg, err = RegExp. Compile (' 0 [thousand picks] ') str = Reg. Replaceallstring (str, "0") Reg, err = RegExp. Compile (' 0 {2,} ') str = Reg. Replaceallstring (str, "0") Reg, err = RegExp. Compile (' 0 ') str = Reg. Replaceallstring (str, "billion") Reg, err = RegExp. Compile (' 0 ') str = Reg. Replaceallstring (str, "million") Reg, Err = Regexp.compile (' 0 * yuan ') str = Reg. Replaceallstring (str, "meta") Reg, Err = Regexp.compile (' billion zero {0, 3} million ') str = Reg. Replaceallstring (str, "^ Yuan") Reg, Err = Regexp.compile (' 0 yuan ') str = Reg. Replaceallstring (str, "0") if err! = Nil {log. Fatal (ERR)}return Str}func main () {FMT. Println ("12345.78 capitalized amount:", Convertnumtocny (12345.78))}

The result of the above code execution is as follows (you can use the CONVERTNUMTOCNY function to enter any conversion amount as an argument):

We often need a large amount of data to reduce the efficiency of our reading data, you can use Golang to add Chinese identity segmentation to increase the readability of the digital, the code is as follows:

package mainimport ("strings""strconv""fmt")func ConvertNumToCap(num float64) string{    strnum := strconv.FormatFloat(num, 'f', 2, 64)    capitalSlice := []string{"万","亿","兆"}    index := 0    result := ""    sdivision := strings.Split(strnum,".")    sl := sdivision[0]    if len(sdivision)>1{        result="."+sdivision[1]    }    // slength := len(sl)    for len(sl)>4{        result = capitalSlice[index]+sl[len(sl)-4:] + result        index = index+1        sl = sl[0:len(sl)-4]    }    result = sl+result    result = strings.Replace(result,"万0000","万",-1)    result = strings.Replace(result,"亿0000","亿",-1)    result = strings.Replace(result,"兆0000","兆",-1)    result = strings.Replace(result,"亿万","亿",-1)    result = strings.Replace(result,"兆亿","兆",-1)    return result}func main() {   fmt.Println("123450000.78的金额转换为:",ConvertNumToCap(123450000.78))}

The result of the above code execution is as follows (you can use the Convertnumtocap function to enter any conversion amount as an argument):

Related Article

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.