package httpsfv
import (
"errors"
"io"
"math"
"strconv"
"strings"
)
const maxDecDigit = 3
var ErrInvalidDecimal = errors .New ("the integer portion is larger than 12 digits: invalid decimal" )
func marshalDecimal(b io .StringWriter , d float64 ) error {
const TH = 0.001
rounded := math .RoundToEven (d /TH ) * TH
i , frac := math .Modf (rounded )
if i < -999999999999 || i > 999999999999 {
return ErrInvalidDecimal
}
if _ , err := b .WriteString (strings .TrimRight (strconv .FormatFloat (rounded , 'f' , 3 , 64 ), "0" )); err != nil {
return err
}
if frac == 0 {
_ , err := b .WriteString ("0" )
return err
}
return nil
}
func parseDecimal(s *scanner , decSepOff int , str string , neg bool ) (float64 , error ) {
if decSepOff == s .off -1 {
return 0 , &UnmarshalError {s .off , ErrInvalidDecimalFormat }
}
if len (s .data [decSepOff +1 :s .off ]) > maxDecDigit {
return 0 , &UnmarshalError {s .off , ErrNumberOutOfRange }
}
i , err := strconv .ParseFloat (str , 64 )
if err != nil {
return 0 , &UnmarshalError {s .off , err }
}
if neg {
i = -i
}
return i , nil
}
The pages are generated with Golds v0.8.2 . (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 @zigo_101 (reachable from the left QR code) to get the latest news of Golds .