DEV Community

Cover image for 7 ways to convert a String to Number in JavaScript

7 ways to convert a String to Number in JavaScript

Sanchithasr on December 16, 2020

1. Using parseInt() parseInt() parses a string and returns a whole number. Spaces are allowed. Only the first number is returned. This m...
Collapse
 
rvxlab profile image
RVxLab • Edited

The double tilde method is actually something I never thought about, but is worth explaining.

The double tilde "operator" is not as much as an operator as it's a double bitwise negation.

Let's cast '64' to 64 using this method, so we do ~~'64'. First we will evaluate ~'64'. As bitwise operations work on binary, '64' is cast to a number. So ~64.

64 in binary is 01000000. ~ will negate all the bits so it becomes 10111111, which is -65 since numbers in JavaScript are signed. Now we negate it again, which becomes 01000000, which is 64 in decimal.

Correction:

I previously stated that 10111111 is -63, which is incorrect. It's actually -65. Sorry about that.

Collapse
 
raddevus profile image
raddevus

Thanks for explaining this. When I got to that one, I was wondering what it was. Great explanation.

Collapse
 
sanchithasr profile image
Sanchithasr • Edited

Thank you. This was informative. I didn't pay attention about how it works while writing. Will note it down.

Collapse
 
jgburet profile image
Jean-Guillaume Buret

You should always pass the base as second argument to parseInt.

Collapse
 
fitgag profile image
FitGAG

This is a really helpful article! As a beginner in JavaScript, I often find myself struggling with converting strings to numbers. It's great to see all these different methods laid out with examples for each one. I especially appreciated the explanation of the parseFloat() function and the caution against using the unary plus operator on strings that could be mistaken for numbers. Thank you for sharing! FitGAG

Collapse
 
juaneme8 profile image
Juan Ocho

In the first paragraph "If you parse the decimal number, it will be rounded off to the nearest integer value and that value is converted to string. " I think it should say "that value is converted to a number".

Collapse
 
tetile profile image
tetile

Cupcake 2048 is a delightful twist on the classic 2048 game, where you combine cupcakes to create even more delicious treats. It's a fun and addictive way to pass the time! You can play it 2048 cupcakes.

Enjoy the sweetness of 2048 cupcakes!

Collapse
 
leroydo profile image
mitchell maurice

The headline says: "If candy clicker parse the decimal number, it will be rounded off to the nearest integer value and that value is converted to string." I believe that the statement "That value is converted to a number" should be added.

Collapse
 
clevewren32 profile image
clevewren

Choosing the best method depends on your specific needs. If you need an integer, use parseInt. If you need a floating-point number, use parseFloat. Number() is a good general-purpose option, while the unary plus operator and multiplication by 1 are convenient for quick conversions but might have unexpected behavior. Remember to handle potential NaN results for robustness. Slope 3 is so addictive! I can't stop trying to beat my high score

Collapse
 
__junaidshah profile image
Junaid

how would we go about converting a 19 digit string to a number , js behaves very weirdly for that
e.g lets say this is the number 6145390195186705543 and want to convert it into number what it gives me is this 6145390195186705000. not sure why

Collapse
 
aalphaindia profile image
Pawan Pawar

thanks for the info.

Collapse
 
himanshupal0001 profile image
Himanshupal0001

Noice

Collapse
 
sammy__jnr profile image
갈 순헤에

thanks

Collapse
 
yeasin2002 profile image
Md Kawsar Islam Yeasin

Great explanation.
Thanks! 🥰💖

Collapse
 
lazytyper profile image
Egon Schmid • Edited

Using x - 0 works, too

Collapse
 
adammagestore profile image
adam pham

For the string that includes comma (VD: '1,234.44'), you can use parseNumber function to avoid the further bug

Collapse
 
writing2 profile image
ESSAY WRITING

Thanks very helpful 👍👌

Collapse
 
sigitnur profile image
Sigit

nice info. some time forget about this and using lib js

Collapse
 
nathan-wells profile image
Nathan wells

I like the Using parseInt() method for generating numbers.

mbox converter

Collapse
 
brettcooperr profile image
Brett Cooper

Thanks for the info dude,

Collapse
 
tripleiconsulting profile image
tripleiconsulting

Many thanks for this!