Skip to content

Commit ace4cd4

Browse files
authored
Fix float formatting (#2018)
Format floats the same way as actions/runner (precision 15, remove trailing zeroes) See: https://github.com/actions/runner/blob/67d70803a95fca2fc86d89231acbc319f9a9be2a/src/Sdk/DTObjectTemplating/ObjectTemplating/Tokens/NumberToken.cs#L34
1 parent 99067a9 commit ace4cd4

2 files changed

Lines changed: 2 additions & 1 deletion

File tree

pkg/exprparser/functions_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ func TestFunctionFormat(t *testing.T) {
230230
{"format('{0', '{1}', 'World')", nil, "Unclosed brackets. The following format string is invalid: '{0'", "format-invalid-format-string"},
231231
{"format('{2}', '{1}', 'World')", "", "The following format string references more arguments than were supplied: '{2}'", "format-invalid-replacement-reference"},
232232
{"format('{2147483648}')", "", "The following format string is invalid: '{2147483648}'", "format-invalid-replacement-reference"},
233+
{"format('{0} {1} {2} {3}', 1.0, 1.1, 1234567890.0, 12345678901234567890.0)", "1 1.1 1234567890 1.23456789012346E+19", nil, "format-floats"},
233234
}
234235

235236
env := &EvaluationEnvironment{

pkg/exprparser/interpreter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ func (impl *interperterImpl) coerceToString(value reflect.Value) reflect.Value {
447447
} else if math.IsInf(value.Float(), -1) {
448448
return reflect.ValueOf("-Infinity")
449449
}
450-
return reflect.ValueOf(fmt.Sprint(value))
450+
return reflect.ValueOf(fmt.Sprintf("%.15G", value.Float()))
451451

452452
case reflect.Slice:
453453
return reflect.ValueOf("Array")

0 commit comments

Comments
 (0)