From bb982c644b8e3e8a59cf2c60b28ff91741c7dc9b Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 27 Apr 2020 08:33:03 -0700 Subject: `$(a: float)` now works consistently in nim js, avoiding printing floats as ints (#14134) * fix https://github.com/timotheecour/Nim/issues/133; $(a: float) works in nim js like in other backends * fix tests * fix test for windows that prints 1.1e17 differently than other OS --- lib/system/jssys.nim | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib/system') diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 2f3512cfc..ec55733e1 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -476,6 +476,21 @@ proc negInt(a: int): int {.compilerproc.} = proc negInt64(a: int64): int64 {.compilerproc.} = result = a*(-1) +proc nimFloatToString(a: float): cstring {.compilerproc.} = + ## ensures the result doesn't print like an integer, ie return 2.0, not 2 + asm """ + function nimOnlyDigitsOrMinus(n) { + return n.toString().match(/^-?\d+$/); + } + if (Number.isSafeInteger(`a`)) `result` = `a`+".0" + else { + `result` = `a`+"" + if(nimOnlyDigitsOrMinus(`result`)){ + `result` = `a`+".0" + } + } + """ + proc absInt(a: int): int {.compilerproc.} = result = if a < 0: a*(-1) else: a -- cgit 1.4.1-2-gfad0