formatFloat takes an optional decimalSep parameter
This commit is contained in:
parent
940b3ceca8
commit
1e597c6332
2 changed files with 19 additions and 10 deletions
|
|
@ -1197,8 +1197,8 @@ proc editDistance*(a, b: string): int {.noSideEffect,
|
||||||
|
|
||||||
# floating point formating:
|
# floating point formating:
|
||||||
|
|
||||||
proc c_sprintf(buf, frmt: cstring) {.header: "<stdio.h>", importc: "sprintf",
|
proc c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>",
|
||||||
varargs, noSideEffect.}
|
importc: "sprintf", varargs, noSideEffect.}
|
||||||
|
|
||||||
type
|
type
|
||||||
FloatFormatMode* = enum ## the different modes of floating point formating
|
FloatFormatMode* = enum ## the different modes of floating point formating
|
||||||
|
|
@ -1209,7 +1209,8 @@ type
|
||||||
{.deprecated: [TFloatFormat: FloatFormatMode].}
|
{.deprecated: [TFloatFormat: FloatFormatMode].}
|
||||||
|
|
||||||
proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
||||||
precision: range[0..32] = 16): string {.
|
precision: range[0..32] = 16;
|
||||||
|
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.
|
||||||
##
|
##
|
||||||
|
|
@ -1225,6 +1226,7 @@ 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]
|
||||||
|
L: cint
|
||||||
frmtstr[0] = '%'
|
frmtstr[0] = '%'
|
||||||
if precision > 0:
|
if precision > 0:
|
||||||
frmtstr[1] = '#'
|
frmtstr[1] = '#'
|
||||||
|
|
@ -1232,15 +1234,20 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
||||||
frmtstr[3] = '*'
|
frmtstr[3] = '*'
|
||||||
frmtstr[4] = floatFormatToChar[format]
|
frmtstr[4] = floatFormatToChar[format]
|
||||||
frmtstr[5] = '\0'
|
frmtstr[5] = '\0'
|
||||||
c_sprintf(buf, frmtstr, precision, f)
|
L = c_sprintf(buf, frmtstr, precision, f)
|
||||||
else:
|
else:
|
||||||
frmtstr[1] = floatFormatToChar[format]
|
frmtstr[1] = floatFormatToChar[format]
|
||||||
frmtstr[2] = '\0'
|
frmtstr[2] = '\0'
|
||||||
c_sprintf(buf, frmtstr, f)
|
L = c_sprintf(buf, frmtstr, f)
|
||||||
result = $buf
|
result = newString(L)
|
||||||
|
for i in 0 ..< L:
|
||||||
|
# Depending on the locale either dot or comma is produced,
|
||||||
|
# but nothing else is possible:
|
||||||
|
if buf[i] in {'.', ','}: result[i] = decimalsep
|
||||||
|
else: result[i] = buf[i]
|
||||||
|
|
||||||
proc formatFloat*(f: float, format: FloatFormatMode = ffDefault,
|
proc formatFloat*(f: float, format: FloatFormatMode = ffDefault,
|
||||||
precision: range[0..32] = 16): string {.
|
precision: range[0..32] = 16; 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.
|
||||||
##
|
##
|
||||||
|
|
@ -1250,7 +1257,7 @@ 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.
|
||||||
result = formatBiggestFloat(f, format, precision)
|
result = formatBiggestFloat(f, format, precision, decimalSep)
|
||||||
|
|
||||||
proc formatSize*(bytes: BiggestInt, decimalSep = '.'): string =
|
proc formatSize*(bytes: BiggestInt, decimalSep = '.'): string =
|
||||||
## Rounds and formats `bytes`. Examples:
|
## Rounds and formats `bytes`. Examples:
|
||||||
|
|
@ -1464,8 +1471,8 @@ when isMainModule:
|
||||||
doAssert wordWrap(inp, 10, false) == outp
|
doAssert wordWrap(inp, 10, false) == outp
|
||||||
|
|
||||||
doAssert formatBiggestFloat(0.00000000001, ffDecimal, 11) == "0.00000000001"
|
doAssert formatBiggestFloat(0.00000000001, ffDecimal, 11) == "0.00000000001"
|
||||||
doAssert formatBiggestFloat(0.00000000001, ffScientific, 1) in
|
doAssert formatBiggestFloat(0.00000000001, ffScientific, 1, ',') in
|
||||||
["1.0e-11", "1.0e-011"]
|
["1,0e-11", "1,0e-011"]
|
||||||
|
|
||||||
doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
|
doAssert "$# $3 $# $#" % ["a", "b", "c"] == "a c b c"
|
||||||
when not defined(testing):
|
when not defined(testing):
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,8 @@ News
|
||||||
- Define ``nimPinToCpu`` to make the ``threadpool`` use explicit thread
|
- Define ``nimPinToCpu`` to make the ``threadpool`` use explicit thread
|
||||||
affinities. This can speed up or slow down the thread pool; it's up to you
|
affinities. This can speed up or slow down the thread pool; it's up to you
|
||||||
to benchmark it.
|
to benchmark it.
|
||||||
|
- ``strutils.formatFloat`` and ``formatBiggestFloat`` do not depend on the C
|
||||||
|
locale anymore and now take an optional ``decimalSep = '.'`` parameter.
|
||||||
|
|
||||||
|
|
||||||
Language Additions
|
Language Additions
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue