Merge pull request #3591 from yglukhov/float-format-js

fixed float formatting for js
This commit is contained in:
Andreas Rumpf 2015-11-26 17:33:09 +01:00
commit 0fb857f14e
2 changed files with 42 additions and 24 deletions

View file

@ -1344,7 +1344,7 @@ proc editDistance*(a, b: string): int {.noSideEffect,
# floating point formating: # floating point formating:
when not defined(js):
proc c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>", proc c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>",
importc: "sprintf", varargs, noSideEffect.} importc: "sprintf", varargs, noSideEffect.}
@ -1370,6 +1370,21 @@ 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.
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'] const floatFormatToChar: array[FloatFormatMode, char] = ['g', 'f', 'e']
var var
frmtstr {.noinit.}: array[0..5, char] frmtstr {.noinit.}: array[0..5, char]

View file

@ -223,6 +223,9 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
"varres/tvartup"]: "varres/tvartup"]:
test "tests/" & testfile & ".nim" test "tests/" & testfile & ".nim"
for testfile in ["pure/strutils"]:
test "lib/" & testfile & ".nim"
# ------------------------- manyloc ------------------------------------------- # ------------------------- manyloc -------------------------------------------
#proc runSpecialTests(r: var TResults, options: string) = #proc runSpecialTests(r: var TResults, options: string) =
# for t in ["lib/packages/docutils/highlite"]: # for t in ["lib/packages/docutils/highlite"]: