Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

number filter converts null to "0" #6188

Closed
jbedard opened this issue Feb 8, 2014 · 4 comments
Closed

number filter converts null to "0" #6188

jbedard opened this issue Feb 8, 2014 · 4 comments

Comments

@jbedard
Copy link
Contributor

jbedard commented Feb 8, 2014

The doc says the 'number' filter:

Formats a number as text.
If the input is not a number an empty string is returned.

But passing many non-number values (null, "", " ", [], ...) return "0" because isNaN is broken.

I think the simple fix would be making the is-number check at the start of formatNumber() better, or if that is too risky of a change then the documentation should be updated. The jQuery isNumeric implementation might be best:

!isNaN( parseFloat(obj) ) && isFinite( obj )
@sidraval
Copy link

sidraval commented Feb 8, 2014

All unit tests pass after this change. It seems possible that the original behavior is intentional since JS itself treats 'null' like numeric 0.

@tbosch tbosch assigned tbosch and unassigned tbosch Feb 13, 2014
@jbedard
Copy link
Contributor Author

jbedard commented Feb 13, 2014

Personally I would prefer what the doc says where only numbers (possibly in a string) are formatted as numbers, and non numbers return a blank string. Even if that can't be done and null is converted to "0" I think all those other cases should return an empty string.

@sagens42
Copy link
Contributor

We should use isNumber Angular function to check for a number, this is fix for this issue

function isNumber(value){return typeof value === 'number';}

@jbedard
Copy link
Contributor Author

jbedard commented Feb 14, 2014

I thought using isNumber would be too strict, however I see you first called parseFloat. This fixes the original issue I brought up but now number leading strings will work such as "123foobar" where parseFloat returns 123. Was that intentional?

@caitp caitp closed this as completed in cceb455 Feb 15, 2014
khepin pushed a commit to khepin/angular.js that referenced this issue Feb 19, 2014
…ings to the empty string

The previous code for filtering out non-finite numbers was broken, as it would convert `null` to `0`,
as well as arrays.

This change fixes this by converting null/undefined/NaN/Infinity/any object to the empty string.

Closes angular#6188
Closes angular#6261
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants