Skip to content

fine-tuning of toNumber function #4684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2017
Merged

fine-tuning of toNumber function #4684

merged 1 commit into from
Jan 12, 2017

Conversation

mingmingwon
Copy link
Contributor

  1. parseFloat() support only one param, see MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat
  2. maybe it's better to use isNaN() to judge the conversion result?

1. parseFloat() support only one param, see MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat
2. maybe it's better to use isNaN() to judge the conversion result?
const n = parseFloat(val, 10)
return (n || n === 0) ? n : val
const n = parseFloat(val)
return isNaN(n) ? val : n
Copy link
Member

@znck znck Jan 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With regex

if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/.test(val))
      return Number(val);
return val;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regex is also good…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think using a regex is a good idea. parseFloat seems to be the fastest way to convert a string to a number: https://jsperf.com/number-vs-plus-vs-parsefloat

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what

@yyx990803 yyx990803 merged commit 79c1c7f into vuejs:dev Jan 12, 2017
@yyx990803
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants