globalize icon indicating copy to clipboard operation
globalize copied to clipboard

Globalize.parseint("foo") returns 0, should return NaN

Open yucca42 opened this issue 15 years ago • 1 comments

When the string does not contain a recognizable number, Globalize.parseInt returns 0, as opposite to parseInt, which returns NaN. It is essential to return NaN, since otherwise the caller cannot distinguish between invalid data and input data representing the number zero.

The problem is in the function

truncate = function( value ) { return value | 0; };

which is currently used in two contexts: 1) to define Globalize.parseInt so that it truncates the return value of Globalize.parseFloat, and 2) in formatNumber when using the "d" format code.

Minimal fix: change the statement to

truncate = function( value ) { return isNaN(value) ? NaN : value | 0 ; }

yucca42 avatar Jul 06 '11 14:07 yucca42

Can also do: Math.round( value > 0 ? value - 0.5 : value + 0.5 );

EDIT: After running perf tests, I think your solution is the best should this be desired.

http://jsperf.com/javascript-trunc/3

kflorence avatar Jul 16 '11 00:07 kflorence