This commit is contained in:
Andreas Rumpf 2018-04-14 00:40:17 +02:00
commit 50d4224d41
2 changed files with 22 additions and 19 deletions

View file

@ -20,6 +20,9 @@
containing the column of the instantiation.
- ``cookies.setCookie` no longer assumes UTC for the expiration date.
- ``strutils.formatEng`` does not distinguish between ``nil`` and ``""``
strings anymore for its ``unit`` parameter. Instead the space is controlled
by a new parameter ``useUnitSpace``.
#### Breaking changes in the compiler

View file

@ -2144,8 +2144,9 @@ proc formatEng*(f: BiggestFloat,
precision: range[0..32] = 10,
trim: bool = true,
siPrefix: bool = false,
unit: string = nil,
decimalSep = '.'): string {.noSideEffect.} =
unit: string = "",
decimalSep = '.',
useUnitSpace = false): string {.noSideEffect.} =
## Converts a floating point value `f` to a string using engineering notation.
##
## Numbers in of the range -1000.0<f<1000.0 will be formatted without an
@ -2184,7 +2185,7 @@ proc formatEng*(f: BiggestFloat,
## with an exponent rather than an SI prefix, regardless of whether
## `siPrefix` is true.
##
## If `unit` is not nil, the provided unit will be appended to the string
## If `useUnitSpace` is true, the provided unit will be appended to the string
## (with a space as required by the SI standard). This behaviour is slightly
## different to appending the unit to the result as the location of the space
## is altered depending on whether there is an exponent.
@ -2199,7 +2200,7 @@ proc formatEng*(f: BiggestFloat,
## formatEng(4100, siPrefix=true, unit="") == "4.1 k"
## formatEng(4100) == "4.1e3"
## formatEng(4100, unit="V") == "4.1e3 V"
## formatEng(4100, unit="") == "4.1e3 " # Space with unit=""
## formatEng(4100, unit="", useUnitSpace=true) == "4.1e3 " # Space with useUnitSpace=true
##
## `decimalSep` is used as the decimal separator.
var
@ -2267,9 +2268,8 @@ proc formatEng*(f: BiggestFloat,
if p != ' ':
suffix = " " & p
exponent = 0 # Exponent replaced by SI prefix
if suffix == "" and unit != nil:
if suffix == "" and useUnitSpace:
suffix = " "
if unit != nil:
suffix &= unit
if exponent != 0:
result &= "e" & $exponent
@ -2712,18 +2712,18 @@ bar
doAssert formatEng(-52731234, 1, decimalSep=',') == "-52,7e6"
doAssert formatEng(4100, siPrefix=true, unit="V") == "4.1 kV"
doAssert formatEng(4.1, siPrefix=true, unit="V") == "4.1 V"
doAssert formatEng(4.1, siPrefix=true, unit="V", useUnitSpace=true) == "4.1 V"
doAssert formatEng(4.1, siPrefix=true) == "4.1" # Note lack of space
doAssert formatEng(4100, siPrefix=true) == "4.1 k"
doAssert formatEng(4.1, siPrefix=true, unit="") == "4.1 " # Includes space
doAssert formatEng(4.1, siPrefix=true, unit="", useUnitSpace=true) == "4.1 " # Includes space
doAssert formatEng(4100, siPrefix=true, unit="") == "4.1 k"
doAssert formatEng(4100) == "4.1e3"
doAssert formatEng(4100, unit="V") == "4.1e3 V"
doAssert formatEng(4100, unit="") == "4.1e3 " # Space with unit=""
doAssert formatEng(4100, unit="V", useUnitSpace=true) == "4.1e3 V"
doAssert formatEng(4100, unit="", useUnitSpace=true) == "4.1e3 "
# Don't use SI prefix as number is too big
doAssert formatEng(3.1e22, siPrefix=true, unit="a") == "31e21 a"
doAssert formatEng(3.1e22, siPrefix=true, unit="a", useUnitSpace=true) == "31e21 a"
# Don't use SI prefix as number is too small
doAssert formatEng(3.1e-25, siPrefix=true, unit="A") == "310e-27 A"
doAssert formatEng(3.1e-25, siPrefix=true, unit="A", useUnitSpace=true) == "310e-27 A"
block: # startsWith / endsWith char tests
var s = "abcdef"