golang如何删除字符串中的%!d(string=

Bou*_*TAC 2 string format integer go

我有这个:

\n
newStr := fmt.Sprintf("new price: %d\xe2\x82\xac", newPrice)\nfmt.Println(newStr) // new price:  %!d(string=500.00)\xe2\x82\xac\n// except I want: new price: 500,00\xe2\x82\xac\n
Run Code Online (Sandbox Code Playgroud)\n

如何删除字符串末尾的%!d(和 ,这样它就可以像这样)new price: 500,00\xe2\x82\xac

\n

我可以使用一种格式,strings.Replace但我认为这不是一个好的答案。

\n

我的newPrice是一个浮子。

\n

我很确定这已经被问过,但很难用谷歌搜索这类事情。

\n

m0j*_*0j0 6

问题是您%d在调用中使用整数值fmt.Sprintf(),但传递浮点值 (500,00)。

\n

尝试这个:

\n
newStr := fmt.Sprintf("new price: %f\xe2\x82\xac", newPrice)\nfmt.Println(newStr) \n
Run Code Online (Sandbox Code Playgroud)\n

  • 请参阅 [fmt 文档](https://pkg.go.dev/fmt) (4认同)