Golang大数转换为可读/字符串格式。

huangapple go评论77阅读模式
英文:

golang big numbers to readable/ string formats

问题

我从一个API得到了一个JSON响应,使用json.Unmarshal将其保存到一个接口变量后,它的格式如下:

 map[message_num:3 
 task_num:0 
 name: test_room 
 type:my role:member sticky:true unread_num:0 
 room_id:3.190762e+06 ]

我想要获取room_id,但它的格式不可读:

 3.190762e+06

我想要将其格式化为字符串,这样我就可以用它发送POST请求了。

英文:

I have a json response from an API that look like this after I use json. Unmarshal then save it to an interface variable.

 map[message_num:3 
 task_num:0 
 name: test_room 
 type:my role:member sticky:true unread_num:0 
 room_id:3.190762e+06 ]

I want to get the room_id, but its not readable

 3.190762e+06

I want to format this to a string, so I can use it to send a post request.

答案1

得分: 1

你可以从你的JSON中提取room_id,它是一个字符串"3.190762e+06"
然后你可以:

参考这个示例

i, err := strconv.ParseFloat("3.190762e+06", 64)
if err == nil {
    s := fmt.Sprintf("%.0f\n", i)
    fmt.Println(s)
}

输出:

3190762
英文:

You could extract room_id from your JSON as a string "3.190762e+06".
Then you can:

See this example:

i, err := strconv.ParseFloat("3.190762e+06", 64)
if err == nil {
	s := fmt.Sprintf("%.0f\n", i)
	fmt.Println(s)
} 

Output:

3190762

huangapple
  • 本文由 发表于 2014年12月27日 15:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/27665349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定