英文:
Avoiding scientific notation
问题
我正在使用Go与数据库进行交互的实验,并在处理十进制字段时遇到问题。
在这个数据库中,大多数基本上是整数字段的字段被定义为带有0精度的十进制类型... 例如:
- 日期字段以YYYYMMDD格式存储,类型为decimal(8, 0)
- ID号以decimal(9, 0)存储
基本上,任何整数都以带有0精度的十进制类型存储。
我尝试的是填充一个结构体中的字段...
type Record struct {
ID uint
CHANGE_DATE uint
...
}
但是当我从数据库中获取字段时,它们通常以这种格式返回,但只有当数字足够长时:2.0141208e+07
我发现我可以将其扫描到一个浮点数中,然后将浮点数转换为无符号整数,如下所示...
mydate := float32(0)
for rows.Next() {
r := Record{}
row.Scan(&.ID, &mydate)
myrecord.Change_date = uint(mydate)
}
如果ID是一个足够大的数字,那么ParseInt会失败,我发现我必须像上面显示的那样进行浮点数/整数转换。
由于我有很多字段(几乎都是数字),我需要进行这种类型转换,所以我想知道是否有更好的方法来进行这种类型转换?
值得一提的是,这是一个来自打包的ERP系统的数据库,所以无法更改表定义,而且我不只是将所有字段都定义为Go中的浮点数,因为我正在尝试输出JSON,而在JSON输出中我会得到科学计数法。
英文:
I am experimenting using Go to interact with a Database and am running into
issues when dealing with Decimal fields.
In this database most fields that are basically integer fields are
typed as decimal with 0 precision... for example:
- Date fields are stored in YYYYMMDD format as decimal(8, 0)
- Id numbers are stored as decimal(9, 0)
Basically any int is stored as a decimal with 0 precision.
What I am attempting is to fill fields in a struct....
type Record struct {
ID uint
CHANGE_DATE uint
...
}
But when I get the fields from the database, they often come back in a
format like this but only if the number is long enough: 2.0141208e+07
I have found that I can Scan into a float and then convert the float to
an uint like this..
mydate := float32(0)
for rows.Next() {
r := Record{}
row.Scan(&.ID, &mydate)
myrecord.Change_date = uint(mydate)
}
If the ID is a large enough number then ParseInt fails and I find I have to
do the float/int conversion as shown above.
Since I have many fields (almost all numbers) that I would need to do this with,
I am wondering if there is a better way to go about this type conversion?
It may also be worth mentioning that this is a database from a packaged ERP
system, so changing the table definitions is not an option and the reason
I don't just make all fields float in Go is that I am trying to output json
and I get the scientific notation in the json output.
答案1
得分: 1
当我从RPC服务器解析JSON时,遇到了相同的问题。我创建了一个函数来将科学计数法字符串转换回uint,代码如下:
func scientificNotationToUInt(scientificNotation string) (uint, error) {
flt, _, err := big.ParseFloat(scientificNotation, 10, 0, big.ToNearestEven)
if err != nil {
return 0, err
}
fltVal := fmt.Sprintf("%.0f", flt)
intVal, err := strconv.ParseInt(fltVal, 10, 64)
if err != nil {
return 0, err
}
return uint(intVal), nil
}
英文:
I encountered the same issue when parsing JSON from a RPC server. I created a function to convert the scientific notation string back to uint like this:
func scientificNotationToUInt(scientificNotation string) (uint, error) {
flt, _, err := big.ParseFloat(scientificNotation, 10, 0, big.ToNearestEven)
if err != nil {
return 0, err
}
fltVal := fmt.Sprintf("%.0f", flt)
intVal, err := strconv.ParseInt(fltVal, 10, 64)
if err != nil {
return 0, err
}
return uint(intVal), nil
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论