0

Lets say I have a string.

const myStr = "-0";

I want to check the sign of this numeric string. I am doing in this way

Math.sign(Number(myStr)) === -1 || Math.sign(Number(myStr)) === -0

But the number function is converting -0 as 0 because of that Math.sign(Number(myStr)) === -0 is giving false. I want to why Number() is behaving like this and is there any way so that the sign of numeric string can be preserved after the converting to number.

  • What do you expect to happen? `-0` is the same as `0`! – 0stone0 Dec 29 '20 at 12:54
  • Is there a meaningful difference between `0` and `-0`? `-0 === 0` is `true` in Firefox. – connexo Dec 29 '20 at 12:54
  • 7
    `Number("-0")` is `-0` and `Math.sign(Number("-0")) === -0` is `true` for me, where are you running this? – Alex K. Dec 29 '20 at 12:54
  • 1
    Which browser / runtime? – Zoli Szabó Dec 29 '20 at 12:54
  • @AlexK. is correct [here is an example](https://jsbin.com/kuzucuvamo/1/edit?js,console) – VLAZ Dec 29 '20 at 12:57
  • 1
    Can you edit the question and shape your code into a runnable snippet that illustrates the issue? I tried doing so but it was working for me. It's possible that the issue lies somewhere else (for instance, `myStr` containing a different value that you think). – Álvaro González Dec 29 '20 at 13:02

0 Answers0