summary refs log tree commit diff stats
path: root/lib/system
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-04-27 08:33:03 -0700
committerGitHub <noreply@github.com>2020-04-27 17:33:03 +0200
commitbb982c644b8e3e8a59cf2c60b28ff91741c7dc9b (patch)
treea922e3a355509a5d12fe7848a1e6c9f5ba3b2386 /lib/system
parent664cb2c0be4e31d40cd3a5a2b9013f1afe47df97 (diff)
downloadNim-bb982c644b8e3e8a59cf2c60b28ff91741c7dc9b.tar.gz
`$(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
Diffstat (limited to 'lib/system')
-rw-r--r--lib/system/jssys.nim15
1 files changed, 15 insertions, 0 deletions
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