在Go中打印一个带有(逗号)千位分隔符的数字

package main
import (
    "fmt"
    "golang.org/x/text/language"
    "golang.org/x/text/message"
)
func main() {
    number := 1_000_000.23456
    p := message.NewPrinter(language.English)
    withCommaThousandSep := p.Sprintf("%f", number)
    fmt.Println(withCommaThousandSep)
}
1,000,000.234560


package main
import (
    "fmt"
    "golang.org/x/text/language"
    "golang.org/x/text/message"
)
func main() {
    number := 1_000_000.23456
    langs := []language.Tag{
        language.German,
        language.Ukrainian,
        language.Chinese,
        language.Arabic,
        language.French,
    }
    for _, l := range langs {
        p := message.NewPrinter(l)
        withCommaThousandSep := p.Sprintf("%s %f", l, number)
        fmt.Println(withCommaThousandSep)
    }
}

de 1.000.000,234560
uk 1 000 000,234560
zh 1,000,000.234560
ar ١٬٠٠٠٬٠٠٠٫٢٣٤٥٦٠
fr 1 000 000,234560


标签: golang
2022.3.25   /   热度:1512   /   分类: golang

评论:

岁寒
2022-10-05 23:43
卧槽,搜技术文章搜到了你的博客,发现你用的是我开发并且还在用的emlog主题,打开的那一刻我瞳孔地震????

发表评论:

©地球仪的BLOG  |  Powered by Emlog