How And When To Ask Clients What Their Budget Is
March 7, 2022Help! I’ve Had To SSH Into A Linux Box And Don’t Know What To Do
April 5, 2022
Ways to convert a string to a number.
- ParseInt()
ParseInt allows for spaces. One downside is that a decimal number passed in will be rounded off to the nearest decimal point and converted to a number.
- Number()
Number() could be used instead of parseInt() because it can convert a decimal number without rounding it down. Returns NAN when value cannot be converted.
- The Unary Operator
Add the plus operator before any string value and it will be converted to a number.
- ParseFloat()
Works better with decimal numbers. Will return NAN if conversion is not possible and only converts the first number when multiple string values are passed in.
- floor()
Using Math.floor() also isn’t kind to decimal conversions because it returns the largest number less than or equals to the number passed in.
- Multiplication
Simply multiplying your string with 1, converts it to a number. It’s also decimal friendly.
- Double Tilde
A double tilde (~~) works the same as the unary operator (+), except it will round down any decimal number.
Happy coding 🙂