NumberFormat — 以千位分隔符方式格式化一个数字。

NumberFormat

// number_format()
// decimals: Sets the number of decimal points.
// decPoint: Sets the separator for the decimal point.
// thousandsSep: Sets the thousands separator.
func NumberFormat(number float64, decimals uint, decPoint, thousandsSep string) string {
    neg := false
    if number < 0 {
        number = -number
        neg = true
    }
    dec := int(decimals)
    // Will round off
    str := fmt.Sprintf("%."+strconv.Itoa(dec)+"F", number)
    prefix, suffix := "", ""
    if dec > 0 {
        prefix = str[0:len(str)-(dec+1)]
        suffix = str[len(str)-dec:]
    } else {
        prefix = str
    }
    sep := []byte(thousandsSep)
    n, l1, l2 := 0, len(prefix), len(sep)
    // thousands sep num
    c := (l1 - 1) / 3
    tmp := make([]byte, l2*c+l1)
    pos := len(tmp) - 1
    for i := l1 - 1; i >= 0; i, n, pos = i-1, n+1, pos-1 {
        if l2 > 0 && n > 0 && n%3 == 0 {
            for j, _ := range sep {
                tmp[pos] = sep[l2-j-1]
                pos--
            }
        }
        tmp[pos] = prefix[i]
    }
    s := string(tmp)
    if dec > 0 {
        s += decPoint + suffix
    }
    if neg {
        s = "-" + s
    }

    return s
}

 

Github地址

https://github.com/syyongx/php2go