diff --git a/changelog.md b/changelog.md index 893ebf3e93..bbfee63f91 100644 --- a/changelog.md +++ b/changelog.md @@ -74,5 +74,9 @@ This now needs to be written as: var a = -5 for i in a..b: echo i + +- ``formatFloat``/``formatBiggestFloat`` now support formatting floats with zero + precision digits. The previous ``precision = 0`` behavior (default formatting) + is now available via ``precision = -1``. - Removed deprecated romans module from the stdlib and published it as separate Nimble package. diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 5c07757cb9..4434ef27bb 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -1927,7 +1927,7 @@ type {.deprecated: [TFloatFormat: FloatFormatMode].} proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault, - precision: range[0..32] = 16; + precision: range[-1..32] = 16; decimalSep = '.'): string {. noSideEffect, rtl, extern: "nsu$1".} = ## Converts a floating point value `f` to a string. @@ -1939,7 +1939,7 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault, ## `precision`'s default value is the maximum number of meaningful digits ## after the decimal point for Nim's ``biggestFloat`` type. ## - ## If ``precision == 0``, it tries to format it nicely. + ## If ``precision == -1``, it tries to format it nicely. when defined(js): var res: cstring case format @@ -1961,7 +1961,7 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault, buf {.noinit.}: array[0..2500, char] L: cint frmtstr[0] = '%' - if precision > 0: + if precision >= 0: frmtstr[1] = '#' frmtstr[2] = '.' frmtstr[3] = '*' @@ -1995,7 +1995,7 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault, result.setLen(result.len - 1) proc formatFloat*(f: float, format: FloatFormatMode = ffDefault, - precision: range[0..32] = 16; decimalSep = '.'): string {. + precision: range[-1..32] = 16; decimalSep = '.'): string {. noSideEffect, rtl, extern: "nsu$1".} = ## Converts a floating point value `f` to a string. ## @@ -2006,16 +2006,16 @@ proc formatFloat*(f: float, format: FloatFormatMode = ffDefault, ## `precision`'s default value is the maximum number of meaningful digits ## after the decimal point for Nim's ``float`` type. ## - ## If ``precision == 0``, it tries to format it nicely. + ## If ``precision == -1``, it tries to format it nicely. ## ## Examples: ## ## .. code-block:: nim ## ## let x = 123.456 - ## echo x.formatFloat() # 123.4560000000000 - ## echo x.formatFloat(ffDecimal, 4) # 123.4560 - ## echo x.formatFloat(ffScientific, 2) # 1.23e+02 + ## doAssert x.formatFloat() == "123.4560000000000" + ## doAssert x.formatFloat(ffDecimal, 4) == "123.4560" + ## doAssert x.formatFloat(ffScientific, 2) == "1.23e+02" ## result = formatBiggestFloat(f, format, precision, decimalSep) @@ -2471,11 +2471,14 @@ when isMainModule: outp = " this is a\nlong text\n--\nmuchlongerthan10chars\nand here\nit goes" doAssert wordWrap(inp, 10, false) == outp + doAssert formatBiggestFloat(1234.567, ffDecimal, -1) == "1234.567000" + doAssert formatBiggestFloat(1234.567, ffDecimal, 0) == "1235." + doAssert formatBiggestFloat(1234.567, ffDecimal, 1) == "1234.6" doAssert formatBiggestFloat(0.00000000001, ffDecimal, 11) == "0.00000000001" doAssert formatBiggestFloat(0.00000000001, ffScientific, 1, ',') in ["1,0e-11", "1,0e-011"] # bug #6589 - doAssert formatFloat(123.456, ffScientific, precision=0) in + doAssert formatFloat(123.456, ffScientific, precision = -1) in ["1.234560e+02", "1.234560e+002"] doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"