adamwdraper / Numeral-js

A javascript library for formatting and manipulating numbers.
http://numeraljs.com
MIT License
9.66k stars 930 forks source link

numeral format for small numbers returns NaN #596

Open meafmira opened 6 years ago

meafmira commented 6 years ago

For example numeral(0.0000000048).format('0') // NaN

dochathuc commented 6 years ago

https://github.com/adamwdraper/Numeral-js/issues/597

vcarl commented 6 years ago

Same issue, appears to be any number with 6 or more leading 0s.

numeral(.0000001).format('0')
> "NaN"
numeral(.000001).format('0')
> "0"
numeral(.0000005).format('0')
> "NaN"
numeral(.0000009).format('0')
> "NaN"
numeral(.0000010).format('0')
> "0"
numeral(.0000015).format('0')
> "0"

Incidentally, that appears to be when the chrome console starts displaying numbers in exponential form.

.000001
> 0.000001
.0000001
> 1e-7
rudolph9 commented 5 years ago

I downgraded to 1.5.6 to avoid this bug.

MrCsabaToth commented 4 years ago

Is this duplicate of #512 ?

zucker-rdrg commented 4 years ago

Same for large and exponential number format.

As a workaround I suggest downgrade to 2.0.4 2.0.6: https://jsfiddle.net/xkp1wmb4/ 2.0.4: https://jsfiddle.net/d9pfv4na/2/

MrCsabaToth commented 4 years ago

I've seen at least three PRs in the queue addressing this issue

code-press commented 3 years ago

Still baffled that the PR's fixing this have not been merged

MrCsabaToth commented 3 years ago

Still baffled that the PR's fixing this have not been merged

Unfortunately the last commit was 4 years ago. The author probably doesn't have time any more for this project.

am05mhz commented 2 years ago

since this repo is not updated anymore, and still have this bug, and if you still wish to use the latest version, you can do this work around:

function formatSmall(val, format) {
  if (val > 0 && val < 1e-6){
    // make the number 100 times greater, then just replace 0.0 with additional 2 zeroes (from 100)
    return numeral(val * 100).format(format).replace('0.0', '0.000');
  }
  return numeral(val).format(format);
}