// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package jximport ()// Float writes float value to buffer.func ( *Writer) ( float64, int) bool {ifmath.IsNaN() || math.IsInf(, 0) {// Like in ECMA: // NaN and Infinity regardless of sign are represented // as the String null. // // JSON.stringify({"foo":NaN}) -> {"foo":null}return .Null() }switch := .stream; {case == nil: .Buf = floatAppend(.Buf, , )returnfalsecase .fail():returntruedefault: := make([]byte, 0, 32) = floatAppend(, , )returnwriteStreamByteseq(, ) }}func ( []byte, float64, int) []byte {// From go std sources, strconv/ftoa.go:// Convert as if by ES6 number to string conversion. // This matches most other JSON generators. // See golang.org/issue/6384 and golang.org/issue/14135. // Like fmt %g, but the exponent cutoffs are different // and exponents themselves are not padded to two digits. := math.Abs() := byte('f')// Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.if != 0 {if == 64 && ( < 1e-6 || >= 1e21) || == 32 && (float32() < 1e-6 || float32() >= 1e21) { = 'e' } } = strconv.AppendFloat(, , , -1, )if == 'e' {// clean up e-09 to e-9 := len()if >= 4 && [-4] == 'e' && [-3] == '-' && [-2] == '0' { [-2] = [-1] = [:-1] } }return}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.