我想在index.html文件中将float64的值格式化为2个小数位。在.go文件中,我可以格式化为:
index.html
float64
.go
strconv.FormatFloat(value, 'f', 2, 32)
字符串但我不知道如何在模板格式。我使用gin-gonic/gin框架的后端。任何帮助将不胜感激。谢谢。
gin-gonic/gin
1cosmwyk1#
你有很多选择:
fmt.Sprintf()
n1
String() string
n2
printf
n3
string
n4
请看这个例子:
type MyFloat float64func (mf MyFloat) String() string { return fmt.Sprintf("%.2f", float64(mf))}func main() { t := template.Must(template.New("").Funcs(template.FuncMap{ "MyFormat": func(f float64) string { return fmt.Sprintf("%.2f", f) }, }).Parse(templ)) m := map[string]interface{}{ "n0": 3.1415, "n1": fmt.Sprintf("%.2f", 3.1415), "n2": MyFloat(3.1415), "n3": 3.1415, "n4": 3.1415, } if err := t.Execute(os.Stdout, m); err != nil { fmt.Println(err) }}const templ = `Number: n0 = {{.n0}}Formatted: n1 = {{.n1}}Custom type: n2 = {{.n2}}Calling printf: n3 = {{printf "%.2f" .n3}}MyFormat: n4 = {{MyFormat .n4}}`
type MyFloat float64
func (mf MyFloat) String() string {
return fmt.Sprintf("%.2f", float64(mf))
}
func main() {
t := template.Must(template.New("").Funcs(template.FuncMap{
"MyFormat": func(f float64) string { return fmt.Sprintf("%.2f", f) },
}).Parse(templ))
m := map[string]interface{}{
"n0": 3.1415,
"n1": fmt.Sprintf("%.2f", 3.1415),
"n2": MyFloat(3.1415),
"n3": 3.1415,
"n4": 3.1415,
if err := t.Execute(os.Stdout, m); err != nil {
fmt.Println(err)
const templ = `
Number: n0 = {{.n0}}
Formatted: n1 = {{.n1}}
Custom type: n2 = {{.n2}}
Calling printf: n3 = {{printf "%.2f" .n3}}
MyFormat: n4 = {{MyFormat .n4}}`
字符串输出(在Go Playground上尝试):
Number: n0 = 3.1415Formatted: n1 = 3.14Custom type: n2 = 3.14Calling printf: n3 = 3.14MyFormat: n4 = 3.14
Number: n0 = 3.1415
Formatted: n1 = 3.14
Custom type: n2 = 3.14
Calling printf: n3 = 3.14
MyFormat: n4 = 3.14
型
zmeyuzjn2#
将printf模板内置函数与"%.2f" format一起使用:
"%.2f" format
tmpl := template.Must(template.New("test").Parse(`The formatted value is = {{printf "%.2f" .}}`))tmpl.Execute(os.Stdout, 123.456789)
tmpl := template.Must(template.New("test").Parse(`The formatted value is = {{printf "%.2f" .}}`))
tmpl.Execute(os.Stdout, 123.456789)
字符串Go Playgroung
31moq8wy3#
您可以注册FuncMap。
FuncMap
package mainimport ( "fmt" "os" "text/template")type Tpl struct { Value float64}func main() { funcMap := template.FuncMap{ "FormatNumber": func(value float64) string { return fmt.Sprintf("%.2f", value) }, } tmpl, _ := template.New("test").Funcs(funcMap).Parse(string("The formatted value is = {{ .Value | FormatNumber }}")) tmpl.Execute(os.Stdout, Tpl{Value: 123.45678})}
package main
import (
"fmt"
"os"
"text/template"
)
type Tpl struct {
Value float64
funcMap := template.FuncMap{
"FormatNumber": func(value float64) string {
return fmt.Sprintf("%.2f", value)
},
tmpl, _ := template.New("test").Funcs(funcMap).Parse(string("The formatted value is = {{ .Value | FormatNumber }}"))
tmpl.Execute(os.Stdout, Tpl{Value: 123.45678})
字符串Playground
q1qsirdb4#
编辑:我对舍入/截断的看法是错误的。
%.2f的问题是它不舍入而是截断。我开发了一个基于int 64的decimal类来处理货币,它处理浮点数、字符串解析、JSON等。它将金额存储为64位整数美分。可以很容易地从浮点数创建或转换回浮点数。也可以存储在DB中。https://github.com/strongo/decimal
%.2f
package exampleimport "github.com/strongo/decimal"func Example() { var amount decimal.Decimal64p2; print(amount) // 0 amount = decimal.NewDecimal64p2(0, 43); print(amount) // 0.43 amount = decimal.NewDecimal64p2(1, 43); print(amount) // 1.43 amount = decimal.NewDecimal64p2FromFloat64(23.100001); print(amount) // 23.10 amount, _ = decimal.ParseDecimal64p2("2.34"); print(amount) // 2.34 amount, _ = decimal.ParseDecimal64p2("-3.42"); print(amount) // -3.42}
package example
import "github.com/strongo/decimal"
func Example() {
var amount decimal.Decimal64p2; print(amount) // 0
amount = decimal.NewDecimal64p2(0, 43); print(amount) // 0.43
amount = decimal.NewDecimal64p2(1, 43); print(amount) // 1.43
amount = decimal.NewDecimal64p2FromFloat64(23.100001); print(amount) // 23.10
amount, _ = decimal.ParseDecimal64p2("2.34"); print(amount) // 2.34
amount, _ = decimal.ParseDecimal64p2("-3.42"); print(amount) // -3.42
字符串
4条答案
按热度按时间1cosmwyk1#
你有很多选择:
fmt.Sprintf()
(n1
)String() string
方法,根据你的喜好格式化。这是由模板引擎(n2
)检查和使用的。printf
,并使用自定义格式字符串(n3
)。printf
,这也需要传递格式string
。如果你不想每次都这样做,你可以注册一个自定义函数来做这件事(n4
)。请看这个例子:
字符串
输出(在Go Playground上尝试):
型
zmeyuzjn2#
将
printf
模板内置函数与"%.2f" format
一起使用:字符串
Go Playgroung
31moq8wy3#
您可以注册
FuncMap
。字符串
Playground
q1qsirdb4#
编辑:我对舍入/截断的看法是错误的。
%.2f
的问题是它不舍入而是截断。我开发了一个基于int 64的decimal类来处理货币,它处理浮点数、字符串解析、JSON等。
它将金额存储为64位整数美分。可以很容易地从浮点数创建或转换回浮点数。
也可以存储在DB中。
https://github.com/strongo/decimal
字符串