MCPcopy
hub / github.com/grafana/k6 / stringToRat

Function stringToRat

lib/execution_segment.go:73–86  ·  view source on GitHub ↗

stringToRat is a helper function that tries to convert a string to a rational number while allowing percentage, decimal, and fraction values.

(s string)

Source from the content-addressed store, hash-verified

71// stringToRat is a helper function that tries to convert a string to a rational
72// number while allowing percentage, decimal, and fraction values.
73func stringToRat(s string) (*big.Rat, error) {
74 if before, ok := strings.CutSuffix(s, "%"); ok {
75 num, ok := new(big.Int).SetString(before, 10)
76 if !ok {
77 return nil, fmt.Errorf("'%s' is not a valid percentage", s)
78 }
79 return new(big.Rat).SetFrac(num, big.NewInt(100)), nil
80 }
81 rat, ok := new(big.Rat).SetString(s)
82 if !ok {
83 return nil, fmt.Errorf("'%s' is not a valid percentage, decimal, fraction or interval value", s)
84 }
85 return rat, nil
86}
87
88// NewExecutionSegmentFromString validates the supplied string value and returns
89// the newly created ExecutionSegment or and error from it.

Calls 2

SetStringMethod · 0.80
ErrorfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…