fixed float formatting for js
This commit is contained in:
parent
eaed36092c
commit
2fcc37fd2b
2 changed files with 42 additions and 24 deletions
|
|
@ -1344,8 +1344,8 @@ proc editDistance*(a, b: string): int {.noSideEffect,
|
|||
|
||||
|
||||
# floating point formating:
|
||||
|
||||
proc c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>",
|
||||
when not defined(js):
|
||||
proc c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>",
|
||||
importc: "sprintf", varargs, noSideEffect.}
|
||||
|
||||
type
|
||||
|
|
@ -1370,6 +1370,21 @@ proc formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
|
|||
## after the decimal point for Nim's ``biggestFloat`` type.
|
||||
##
|
||||
## If ``precision == 0``, it tries to format it nicely.
|
||||
when defined(js):
|
||||
var res: cstring
|
||||
case format
|
||||
of ffDefault:
|
||||
{.emit: "`res` = `f`.toString();".}
|
||||
of ffDecimal:
|
||||
{.emit: "`res` = `f`.toFixed(`precision`);".}
|
||||
of ffScientific:
|
||||
{.emit: "`res` = `f`.toExponential(`precision`);".}
|
||||
result = $res
|
||||
for i in 0 ..< result.len:
|
||||
# Depending on the locale either dot or comma is produced,
|
||||
# but nothing else is possible:
|
||||
if result[i] in {'.', ','}: result[i] = decimalsep
|
||||
else:
|
||||
const floatFormatToChar: array[FloatFormatMode, char] = ['g', 'f', 'e']
|
||||
var
|
||||
frmtstr {.noinit.}: array[0..5, char]
|
||||
|
|
|
|||
|
|
@ -223,6 +223,9 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
|
|||
"varres/tvartup"]:
|
||||
test "tests/" & testfile & ".nim"
|
||||
|
||||
for testfile in ["pure/strutils"]:
|
||||
test "lib/" & testfile & ".nim"
|
||||
|
||||
# ------------------------- manyloc -------------------------------------------
|
||||
#proc runSpecialTests(r: var TResults, options: string) =
|
||||
# for t in ["lib/packages/docutils/highlite"]:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue