diff options
-rw-r--r-- | changelog.md | 4 | ||||
-rw-r--r-- | lib/pure/strformat.nim | 4 | ||||
-rw-r--r-- | lib/pure/strutils.nim | 7 | ||||
-rw-r--r-- | tests/destructor/tnewruntime_strutils.nim | 2 | ||||
-rw-r--r-- | tests/stdlib/tstrformat.nim | 31 |
5 files changed, 40 insertions, 8 deletions
diff --git a/changelog.md b/changelog.md index e7ab1a298..4aa22b9d7 100644 --- a/changelog.md +++ b/changelog.md @@ -31,7 +31,11 @@ - The file descriptors created for internal bookkeeping by `ioselector_kqueue` and `ioselector_epoll` will no longer be leaked to child processes. +- `strutils.formatFloat` with `precision = 0` has been restored to the version + 1 behaviour that produces a trailing dot, e.g. `formatFloat(3.14159, precision = 0)` + is now `3.`, not `3`. - `critbits` adds `commonPrefixLen`. + - `relativePath(rel, abs)` and `relativePath(abs, rel)` used to silently give wrong results (see #13222); instead they now use `getCurrentDir` to resolve those cases, and this can now throw in edge cases where `getCurrentDir` throws. diff --git a/lib/pure/strformat.nim b/lib/pure/strformat.nim index 838baaf02..5f7ef380d 100644 --- a/lib/pure/strformat.nim +++ b/lib/pure/strformat.nim @@ -692,11 +692,11 @@ when isMainModule: check &"{-123.456:.3f}", "-123.456" check &"{123.456:1g}", "123.456" check &"{123.456:.1f}", "123.5" - check &"{123.456:.0f}", "123" + check &"{123.456:.0f}", "123." check &"{123.456:>9.3f}", " 123.456" check &"{123.456:9.3f}", " 123.456" check &"{123.456:>9.4f}", " 123.4560" - check &"{123.456:>9.0f}", " 123" + check &"{123.456:>9.0f}", " 123." check &"{123.456:<9.4f}", "123.4560 " # Float (scientific) tests diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index ff76fa94f..a1f80ad76 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -2430,10 +2430,6 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault, # but nothing else is possible: if buf[i] in {'.', ','}: result[i] = decimalSep else: result[i] = buf[i] - since (1, 1): - # remove trailing dot, compatible with Python's formatter and JS backend - if result[^1] == decimalSep: - result.setLen(len(result)-1) when defined(windows): # VS pre 2015 violates the C standard: "The exponent always contains at # least two digits, and only as many more digits as necessary to @@ -2936,7 +2932,8 @@ proc isNilOrWhitespace*(s: string): bool {.noSideEffect, procvar, rtl, when isMainModule: proc nonStaticTests = doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000" - doAssert formatBiggestFloat(1234.567, ffDecimal, 0) == "1235" # bugs 8242, 12586 + when not defined(js): + doAssert formatBiggestFloat(1234.567, ffDecimal, 0) == "1235." # bugs 8242, 12586 doAssert formatBiggestFloat(1234.567, ffDecimal, 1) == "1234.6" doAssert formatBiggestFloat(0.00000000001, ffDecimal, 11) == "0.00000000001" doAssert formatBiggestFloat(0.00000000001, ffScientific, 1, ',') in diff --git a/tests/destructor/tnewruntime_strutils.nim b/tests/destructor/tnewruntime_strutils.nim index 9afb507f7..b0bceb4b5 100644 --- a/tests/destructor/tnewruntime_strutils.nim +++ b/tests/destructor/tnewruntime_strutils.nim @@ -37,7 +37,7 @@ bug12899() proc nonStaticTests = doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000" - doAssert formatBiggestFloat(1234.567, ffDecimal, 0) == "1235" # bugs 8242, 12586 + doAssert formatBiggestFloat(1234.567, ffDecimal, 0) == "1235." # bugs 8242, 12586 doAssert formatBiggestFloat(1234.567, ffDecimal, 1) == "1234.6" doAssert formatBiggestFloat(0.00000000001, ffDecimal, 11) == "0.00000000001" doAssert formatBiggestFloat(0.00000000001, ffScientific, 1, ',') in diff --git a/tests/stdlib/tstrformat.nim b/tests/stdlib/tstrformat.nim index 403372aed..7db001979 100644 --- a/tests/stdlib/tstrformat.nim +++ b/tests/stdlib/tstrformat.nim @@ -173,3 +173,34 @@ block: y = 234 z = true """ + + +# tests from the very own strformat documentation! + +let msg = "hello" +doAssert fmt"{msg}\n" == "hello\\n" + +doAssert &"{msg}\n" == "hello\n" + +doAssert fmt"{msg}{'\n'}" == "hello\n" +doAssert fmt("{msg}\n") == "hello\n" +doAssert "{msg}\n".fmt == "hello\n" + +doAssert &"""{"abc":>4}""" == " abc" +doAssert &"""{"abc":<4}""" == "abc " + +doAssert fmt"{-12345:08}" == "-0012345" +doAssert fmt"{-1:3}" == " -1" +doAssert fmt"{-1:03}" == "-01" +doAssert fmt"{16:#X}" == "0x10" + +doAssert fmt"{123.456}" == "123.456" +doAssert fmt"{123.456:>9.3f}" == " 123.456" +doAssert fmt"{123.456:9.3f}" == " 123.456" +doAssert fmt"{123.456:9.4f}" == " 123.4560" +doAssert fmt"{123.456:>9.0f}" == " 123." +doAssert fmt"{123.456:<9.4f}" == "123.4560 " + +doAssert fmt"{123.456:e}" == "1.234560e+02" +doAssert fmt"{123.456:>13e}" == " 1.234560e+02" +doAssert fmt"{123.456:13e}" == " 1.234560e+02" |