dsdtszi0520538 2014-12-16 19:38 采纳率: 100%
浏览 424

避免科学计数法

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 linux驱动,linux应用,多线程
    • ¥20 我要一个分身加定位两个功能的安卓app
    • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
    • ¥15 IAR程序莫名变量多重定义
    • ¥15 (标签-UDP|关键词-client)
    • ¥15 关于库卡officelite无法与虚拟机通讯的问题
    • ¥15 目标检测项目无法读取视频
    • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
    • ¥100 求采集电商背景音乐的方法
    • ¥15 数学建模竞赛求指导帮助