summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorFabian Keller <bluenote10@users.noreply.github.com>2017-11-15 21:23:33 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-11-15 21:23:33 +0100
commit5e66a7ce591f8abf2cddeffe8389c53a3183c2a8 (patch)
tree2ae9d7476404a29f7357e80f7e745b76bb7d6240 /lib
parent0ab373115c75e75618d3e8935445cee3e0298b60 (diff)
downloadNim-5e66a7ce591f8abf2cddeffe8389c53a3183c2a8.tar.gz
Modified handling of zero precision in formatFloat (#6719)
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/strutils.nim21
1 files changed, 12 insertions, 9 deletions
diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim
index 5c07757cb..4434ef27b 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"