Moved zero trimming into a separate function (code review comment)
This commit is contained in:
parent
d855ad7045
commit
1b2c599854
2 changed files with 23 additions and 29 deletions
|
|
@ -1392,7 +1392,7 @@ type
|
||||||
|
|
||||||
proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
||||||
precision: range[0..32] = 16;
|
precision: range[0..32] = 16;
|
||||||
decimalSep = '.', trim = false): string {.
|
decimalSep = '.'): string {.
|
||||||
noSideEffect, rtl, extern: "nsu$1".} =
|
noSideEffect, rtl, extern: "nsu$1".} =
|
||||||
## Converts a floating point value `f` to a string.
|
## Converts a floating point value `f` to a string.
|
||||||
##
|
##
|
||||||
|
|
@ -1404,8 +1404,6 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
||||||
## after the decimal point for Nim's ``biggestFloat`` type.
|
## after the decimal point for Nim's ``biggestFloat`` type.
|
||||||
##
|
##
|
||||||
## If ``precision == 0``, it tries to format it nicely.
|
## If ``precision == 0``, it tries to format it nicely.
|
||||||
##
|
|
||||||
## If ``trim == true``, trailing zeros will be removed.
|
|
||||||
when defined(js):
|
when defined(js):
|
||||||
var res: cstring
|
var res: cstring
|
||||||
case format
|
case format
|
||||||
|
|
@ -1425,7 +1423,6 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
||||||
var
|
var
|
||||||
frmtstr {.noinit.}: array[0..5, char]
|
frmtstr {.noinit.}: array[0..5, char]
|
||||||
buf {.noinit.}: array[0..2500, char]
|
buf {.noinit.}: array[0..2500, char]
|
||||||
splResult: seq[string]
|
|
||||||
L: cint
|
L: cint
|
||||||
frmtstr[0] = '%'
|
frmtstr[0] = '%'
|
||||||
if precision > 0:
|
if precision > 0:
|
||||||
|
|
@ -1446,21 +1443,8 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
||||||
if buf[i] in {'.', ','}: result[i] = decimalsep
|
if buf[i] in {'.', ','}: result[i] = decimalsep
|
||||||
else: result[i] = buf[i]
|
else: result[i] = buf[i]
|
||||||
|
|
||||||
# Trim trailing zeros if required (used by formatSize)
|
|
||||||
if trim and result.contains(decimalSep):
|
|
||||||
if result.contains('e'):
|
|
||||||
splResult = result.split('e')
|
|
||||||
result = splResult[0]
|
|
||||||
while result[result.high] == '0':
|
|
||||||
result.setLen(result.len-1)
|
|
||||||
if result[result.high] == decimalSep:
|
|
||||||
result.setLen(result.len-1)
|
|
||||||
if splResult.len > 0:
|
|
||||||
result &= "e" & splResult[1]
|
|
||||||
|
|
||||||
proc formatFloat*(f: float, format: FloatFormatMode = ffDefault,
|
proc formatFloat*(f: float, format: FloatFormatMode = ffDefault,
|
||||||
precision: range[0..32] = 16; decimalSep = '.',
|
precision: range[0..32] = 16; decimalSep = '.'): string {.
|
||||||
trim = false): string {.
|
|
||||||
noSideEffect, rtl, extern: "nsu$1".} =
|
noSideEffect, rtl, extern: "nsu$1".} =
|
||||||
## Converts a floating point value `f` to a string.
|
## Converts a floating point value `f` to a string.
|
||||||
##
|
##
|
||||||
|
|
@ -1470,8 +1454,23 @@ proc formatFloat*(f: float, format: FloatFormatMode = ffDefault,
|
||||||
## of significant digits to be printed.
|
## of significant digits to be printed.
|
||||||
## `precision`'s default value is the maximum number of meaningful digits
|
## `precision`'s default value is the maximum number of meaningful digits
|
||||||
## after the decimal point for Nim's ``float`` type.
|
## after the decimal point for Nim's ``float`` type.
|
||||||
## If `trim` is set to true, trailing zeros will be removed.
|
result = formatBiggestFloat(f, format, precision, decimalSep)
|
||||||
result = formatBiggestFloat(f, format, precision, decimalSep, trim)
|
|
||||||
|
proc trimZeros*(x: string): string {.noSideEffect.} =
|
||||||
|
## Trim trailing zeros from a formatted floating point
|
||||||
|
## value (`x`).
|
||||||
|
var splResult: seq[string]
|
||||||
|
result = x
|
||||||
|
if result.contains('.') or result.contains(','):
|
||||||
|
if result.contains('e'):
|
||||||
|
splResult = result.split('e')
|
||||||
|
result = splResult[0]
|
||||||
|
while result[result.high] == '0':
|
||||||
|
result.setLen(result.len-1)
|
||||||
|
if result[result.high] in [',', '.']:
|
||||||
|
result.setLen(result.len-1)
|
||||||
|
if splResult.len > 0:
|
||||||
|
result &= "e" & splResult[1]
|
||||||
|
|
||||||
type
|
type
|
||||||
BinaryPrefixMode* = enum ## the different names for binary prefixes
|
BinaryPrefixMode* = enum ## the different names for binary prefixes
|
||||||
|
|
@ -1527,8 +1526,8 @@ proc formatSize*(bytes: int64,
|
||||||
break
|
break
|
||||||
# xb has the integer number for the latest value; index should be correct
|
# xb has the integer number for the latest value; index should be correct
|
||||||
fbytes = bytes.float / (1'i64 shl (matchedIndex*10)).float
|
fbytes = bytes.float / (1'i64 shl (matchedIndex*10)).float
|
||||||
result = formatFloat(fbytes, format=ffDecimal, precision=3,
|
result = formatFloat(fbytes, format=ffDecimal, precision=3, decimalSep=decimalSep)
|
||||||
decimalSep=decimalSep, trim=true)
|
result = trimZeros(result)
|
||||||
if includeSpace:
|
if includeSpace:
|
||||||
result &= " "
|
result &= " "
|
||||||
result &= prefixes[matchedIndex]
|
result &= prefixes[matchedIndex]
|
||||||
|
|
@ -1729,11 +1728,6 @@ when isMainModule:
|
||||||
|
|
||||||
doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
|
doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
|
||||||
|
|
||||||
block: # formatFloat trim tests
|
|
||||||
doAssert formatFloat(2.33, trim=true) == "2.33"
|
|
||||||
doAssert formatFloat(2.34e50, ffScientific, precision=8, trim=true) in
|
|
||||||
["2.34e+50", "2.34e+050"]
|
|
||||||
|
|
||||||
block: # formatSize tests
|
block: # formatSize tests
|
||||||
doAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB"
|
doAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB"
|
||||||
doAssert formatSize((2.234*1024*1024).int) == "2.234MiB"
|
doAssert formatSize((2.234*1024*1024).int) == "2.234MiB"
|
||||||
|
|
|
||||||
|
|
@ -68,8 +68,8 @@ Library Additions
|
||||||
versions are for ``char`` and ``set[char]``.
|
versions are for ``char`` and ``set[char]``.
|
||||||
- Added ``splitDecimal`` to ``math.nim`` to split a floating point value
|
- Added ``splitDecimal`` to ``math.nim`` to split a floating point value
|
||||||
into an integer part and a floating part (in the range -1<x<1).
|
into an integer part and a floating part (in the range -1<x<1).
|
||||||
- Added ``trim`` argument to ``formatFloat`` in ``strutils.nim`` to trim
|
- Added ``trimZeros`` to ```strutils.nim`` to trim trailing zeros in a
|
||||||
trailing zeros.
|
floating point number.
|
||||||
|
|
||||||
|
|
||||||
Compiler Additions
|
Compiler Additions
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue