Converting text to a number depends on your programming language

When you have text that looks like a number — like "42" or "3.14" — your code often needs to treat it as an actual number to do math or comparisons. Different programming languages have different ways to make this conversion. The method you use depends on what language you're writing in and whether your text contains a whole number or a decimal.

The conversion process is straightforward once you know the right function or method for your language. Most languages have a built-in way to do this, and the conversion either works or fails with an error message if the text doesn't look like a number.

Key Takeaways

  • Python uses int() for whole numbers and float() for decimals, and will show an error if the text isn't a valid number.
  • JavaScript uses parseInt() for whole numbers and parseFloat() for decimals, and returns NaN (not a number) if conversion fails.
  • Java uses Integer.parseInt() for whole numbers and Double.parseDouble() for decimals, and throws an exception if the text is invalid.
  • You should always check whether your text actually contains a number before converting, or handle the error that occurs if it doesn't.
  • Leading and trailing spaces usually don't cause problems, but letters or special characters will cause the conversion to fail.

Converting in Python

Python has two main functions: int() converts text to a whole number, and float() converts text to a decimal number. If the text doesn't look like a number, Python stops and shows an error message.

To convert the text "42" to the number 42, write int("42"). To convert "3.14" to a decimal, write float("3.14"). If you try int("hello"), Python will show a ValueError because "hello" is not a number.

If you're not sure whether the text is a valid number, you can use a try-except block to catch the error. This lets your program keep running instead of crashing:

try:   number = int("42") except ValueError:   print("That's not a number")

Converting in JavaScript

JavaScript uses parseInt() for whole numbers and parseFloat() for decimals. Unlike Python, these functions don't throw an error — instead they return NaN (which stands for "not a number") if the text can't be converted.

Write parseInt("42") to get the number 42, or parseFloat("3.14") to get 3.14. If you write parseInt("hello"), you get NaN instead of an error. You can check for NaN using isNaN(): if isNaN(result) is true, the conversion failed.

One quirk of parseInt: if your text starts with a number but has letters after it, like "42abc", parseInt will return just 42 and ignore the rest. This is useful sometimes but can hide mistakes, so be aware of it.

Converting in Java

Java requires you to specify exactly what type of number you want. Use Integer.parseInt() for whole numbers and Double.parseDouble() for decimals. If the text isn't a valid number, Java throws an exception and stops your program unless you catch it.

Write Integer.parseInt("42") to convert "42" to the number 42. Write Double.parseDouble("3.14") to convert "3.14" to a decimal. If you try Integer.parseInt("hello"), Java throws a NumberFormatException.

To handle the error, wrap the conversion in a try-catch block:

try {   int number = Integer.parseInt("42"); } catch (NumberFormatException e) {   System.out.println("That's not a number"); }

What happens when the text isn't a valid number

Every language handles invalid text differently. Python and Java stop and show an error. JavaScript returns NaN. In all cases, your code won't get the number you expected, so you need to plan for this.

Text that contains spaces at the start or end — like " 42 " — usually converts fine because most languages strip those spaces automatically. Text with letters, symbols, or multiple decimal points will fail. If you're converting text that comes from a user or from a file, always assume some of it might not be a valid number.

The safest approach is to check before converting, or to catch the error if conversion fails. This keeps your program running and lets you decide what to do when the text isn't a number — show a message to the user, use a default value, or try something else.

Converting text with decimals vs. whole numbers

If your text contains a decimal point, you must use the decimal conversion function (float in Python, parseFloat in JavaScript, Double.parseDouble in Java). If you use the whole-number function on "3.14", most languages will either fail or drop the decimal part.

The reverse is also true: if your text is "42" and you use the decimal function, you'll get 42.0, which is fine — the number is the same, just stored as a decimal. But if you then try to store 42.0 in a variable that only holds whole numbers, some languages will complain.

Frequently Asked Questions

What if my text has leading zeros, like "007"?

In most languages, int("007") or parseInt("007") will convert to 7, and the leading zeros are ignored. This is usually what you want. In some older systems or specific contexts, leading zeros mean something special, but in modern programming they're just dropped.

Can I convert negative numbers, like "-42"?

Yes. All the conversion functions handle the minus sign correctly. int("-42") gives you -42, and parseFloat("-3.14") gives you -3.14. The minus sign is part of the number format, so it converts without any special steps.

What's the difference between int() and float() in Python?

int() converts to a whole number and drops anything after the decimal point. float() converts to a decimal number and keeps it. Use int() when you need a whole number, and float() when you need decimals. If you use int("3.14"), Python will show an error — you must use float() first.

Why does parseInt("42abc") return 42 instead of failing?

JavaScript's parseInt stops reading as soon as it hits a character that isn't part of a number. This is sometimes useful but can hide mistakes in your data. If you want to reject text that has anything after the number, check the result or use a different method that validates the entire string.

Should I always use try-catch when converting?

If the text comes from a user, a file, or anywhere you don't fully control, yes — use try-catch or check for NaN. If you're converting text you wrote yourself in the code, you probably don't need it. The safest habit is to always handle the error case, even if you think the text is definitely a number.