Fixed implements formatStringInterpolator.Fixed.
(precision *int)
| 280 | |
| 281 | // Fixed implements formatStringInterpolator.Fixed. |
| 282 | func (c *stringFormatter) Fixed(precision *int) func(ref.Val, string) (string, error) { |
| 283 | if precision == nil { |
| 284 | precision = new(int) |
| 285 | *precision = defaultPrecision |
| 286 | } |
| 287 | return func(arg ref.Val, locale string) (string, error) { |
| 288 | strException := false |
| 289 | if arg.Type() == types.StringType { |
| 290 | argStr := arg.Value().(string) |
| 291 | if argStr == "NaN" || argStr == "Infinity" || argStr == "-Infinity" { |
| 292 | strException = true |
| 293 | } |
| 294 | } |
| 295 | if arg.Type() != types.DoubleType && !strException { |
| 296 | return "", fixedPointFormatError(runtimeID, arg.Type().TypeName()) |
| 297 | } |
| 298 | argFloatVal := arg.ConvertToType(types.DoubleType) |
| 299 | argFloat, ok := argFloatVal.Value().(float64) |
| 300 | if !ok { |
| 301 | return "", fmt.Errorf("could not convert \"%s\" to float64", argFloatVal.Value()) |
| 302 | } |
| 303 | fmtStr := fmt.Sprintf("%%.%df", *precision) |
| 304 | |
| 305 | matchedLocale, err := matchLanguage(locale) |
| 306 | if err != nil { |
| 307 | return "", fmt.Errorf("error matching locale: %w", err) |
| 308 | } |
| 309 | return message.NewPrinter(matchedLocale).Sprintf(fmtStr, argFloat), nil |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Scientific implements formatStringInterpolator.Scientific. |
| 314 | func (c *stringFormatter) Scientific(precision *int) func(ref.Val, string) (string, error) { |
nothing calls this directly
no test coverage detected