1. Problem
Recently, I took over a mall waybill number module. After taking over, I found that some waybill numbers were returned to the front end according to the scientific notation method, such as: 8.0497183772403904E+17. Later, I found that these waybill numbers were imported according to the scientific notation method when importing Excel, and did not do any processing.
Returning the waybill number to the user in the form of scientific notation is too unfriendly and is simply a bug, so it needs to be converted before returning to the user.
2. Problem fix
For Google programming, the following methods were found to convert:
The first type:
oldNum := float64(8.0497183772403904E+17) newNum := (1, 1) newNum.SetFloat64(oldNum) ((0))
result:
804758523075821952
The second type:
var newNum float64 numStr := "8.04758523075822e+17" _, err := (numStr, "%e", &newNum) if err != nil { (" error, numStr:%s, err:%v", numStr, err) return } num := ("%.f", newNum) (num)
result:
804758523075821952
The above two types of reprinted will have some accuracy lost. The specific reason is unknown. I haven’t studied it in depth yet. Friends who know it hope you can comment and reply to answer. Thank you.
The third type:
There is a problem of loss in both of the above two. At first I didn’t know it, but later I converted it through java and found that the java code was really lost as follows:
BigDecimal bd = new BigDecimal("8.04758523075822e+17"); (());
result:
804758523075822000
Since Java can be converted through BigDecimal, golang is definitely OK (unfortunately, I used go1.11.2 but did not find the relevant package support for decimal, so I can only program for github). I found a package used by more people in github /shopspring/decimal
The code used is as follows:
//Instal the package first go get /shopspring/decimalnumStr := "8.04758523075822e+17" decimalNum, err := (numStr) if err != nil { (" error, numStr:%s, err:%v", numStr, err) return } (())
result:
804758523075822000
3. Summary
When performing scientific counting method to convert numbers into string numbers, it is best to use the third method so that the accuracy will not be lost. Of course, the business of importing Excel must be judged. If it is a purely digital waybill number, it should be in the form of pure numbers rather than scientific notation.
package main import ( "encoding/json" "fmt" "/shopspring/decimal" "strconv" //"/shopspring/decimal" ) type abc struct { Goods_id float64 `json:"goods_id"` Goods_name string `json:"goods_name"` } func main(){ a := `{"goods_id":1.1572417766286e+18,"goods_name":"LM358"}` aaa := &abc{} err := ([]byte(a),aaa) if err !=nil{ (err) } (aaa) decimalNum, err :=((aaa.Goods_id, 'e', -1, 64)) if err != nil { return } (()) }
D:\gocode1.14\go-micro-code\gjj>go run
&{1.1572417766286e+18 LM358}
1157241776628600000
This is the article about the implementation of Golang scientific notation conversion string digital output. For more related Golang scientific notation conversion, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!