MCPcopy Create free account
hub / github.com/cel-expr/cel-go / Scientific

Method Scientific

ext/formatting_v2.go:277–312  ·  view source on GitHub ↗

Scientific implements formatStringInterpolatorV2.Scientific.

(precision int)

Source from the content-addressed store, hash-verified

275
276// Scientific implements formatStringInterpolatorV2.Scientific.
277func (c *stringFormatterV2) Scientific(precision int) func(ref.Val) (string, error) {
278 return func(arg ref.Val) (string, error) {
279 fmtStr := fmt.Sprintf("%%1.%de", precision)
280 switch arg.Type() {
281 case types.IntType:
282 argInt, ok := arg.Value().(int64)
283 if !ok {
284 return "", fmt.Errorf("type conversion error from '%s' to '%s'", arg.Type(), types.IntType)
285 }
286 return fmt.Sprintf(fmtStr, float64(argInt)), nil
287 case types.UintType:
288 argUint, ok := arg.Value().(uint64)
289 if !ok {
290 return "", fmt.Errorf("type conversion error from '%s' to '%s'", arg.Type(), types.UintType)
291 }
292 return fmt.Sprintf(fmtStr, float64(argUint)), nil
293 case types.DoubleType:
294 argDbl, ok := arg.Value().(float64)
295 if !ok {
296 return "", fmt.Errorf("type conversion error from '%s' to '%s'", arg.Type(), types.DoubleType)
297 }
298 if math.IsNaN(argDbl) {
299 return "NaN", nil
300 }
301 if math.IsInf(argDbl, -1) {
302 return "-Infinity", nil
303 }
304 if math.IsInf(argDbl, 1) {
305 return "Infinity", nil
306 }
307 return fmt.Sprintf(fmtStr, argDbl), nil
308 default:
309 return "", scientificFormatErrorV2(runtimeID, arg.Type().TypeName())
310 }
311 }
312}
313
314// Binary implements formatStringInterpolatorV2.Binary.
315func (c *stringFormatterV2) Binary(arg ref.Val) (string, error) {

Callers

nothing calls this directly

Calls 4

scientificFormatErrorV2Function · 0.85
TypeMethod · 0.65
ValueMethod · 0.65
TypeNameMethod · 0.65

Tested by

no test coverage detected