...

Source file src/github.com/redis/go-redis/v9/internal/util/convert.go

Documentation: github.com/redis/go-redis/v9/internal/util

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"strconv"
     7  )
     8  
     9  // ParseFloat parses a Redis RESP3 float reply into a Go float64,
    10  // handling "inf", "-inf", "nan" per Redis conventions.
    11  func ParseStringToFloat(s string) (float64, error) {
    12  	switch s {
    13  	case "inf":
    14  		return math.Inf(1), nil
    15  	case "-inf":
    16  		return math.Inf(-1), nil
    17  	case "nan", "-nan":
    18  		return math.NaN(), nil
    19  	}
    20  	return strconv.ParseFloat(s, 64)
    21  }
    22  
    23  // MustParseFloat is like ParseFloat but panics on parse errors.
    24  func MustParseFloat(s string) float64 {
    25  	f, err := ParseStringToFloat(s)
    26  	if err != nil {
    27  		panic(fmt.Sprintf("redis: failed to parse float %q: %v", s, err))
    28  	}
    29  	return f
    30  }
    31  

View as plain text