$(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
This commit is contained in:
parent
664cb2c0be
commit
bb982c644b
4 changed files with 90 additions and 11 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue