我有这个:
newStr := fmt.Sprintf("new price: %d€", newPrice)
fmt.Println(newStr) // new price: %!d(string=500.00)€
// except I want: new price: 500,00€
如何删除字符串末尾的 %!d(
和 )
使其变成这样 new price: 500,00€
我可以使用 strings.Replace
格式,但我认为这不是好的答案。
我的 newPrice
是一个 float 。
我很确定它已经被问过了,但很难用谷歌搜索这类东西。
最佳答案
问题是您在调用 fmt.Sprintf()
时使用 %d
作为整数值,但传递了一个浮点值 (500 ,00).
试试这个:
newStr := fmt.Sprintf("new price: %f€", newPrice)
fmt.Println(newStr)
关于string - golang 如何删除 %!d(string= in string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71488798/