Answers for "golang json float"

0

golang json float

type KeepZero float64

func (f KeepZero) MarshalJSON() ([]byte, error) {
    if float64(f) == float64(int(f)) {
        return []byte(strconv.FormatFloat(float64(f), 'f', 1, 32)), nil
    }
    return []byte(strconv.FormatFloat(float64(f), 'f', -1, 32)), nil
}

type Pt struct {
    Value KeepZero
}

func main() {
    data, err := json.Marshal(Pt{40.0})
    fmt.Println(string(data), err)
}
Posted by: Guest on May-01-2021

Browse Popular Code Answers by Language