How to Convert String to Number Last Updated : 28 Mar, 2024 Comments Improve Suggest changes Like Article Like Report Given a string representation of a numerical value, convert it into an actual numerical value. In this article, we will provide a detailed overview about different ways to convert string to number in different languages. Table of Content Convert String to Number in CConvert String to Number in C++Convert String to Number in JavaConvert String to Number in PythonConvert String to Number in C#Convert String to Number in JavaScriptConvert String to Number in C:1. Using Built-in Functions:Use the built-in function to convert the string to a number. C #include <stdio.h> #include <stdlib.h> int main() { char str[] = "123"; // Step 1 int num = atoi(str); printf("%d\n", num); return 0; } Output123 2. String Concatenation:Check if the string represents a negative number and set a flag.Iterate over each character of the string and convert it to its corresponding numerical value.Calculate the numerical value using the digits. C #include <stdio.h> int main() { char str[] = "-123"; int i = 0, sign = 1, num = 0; // Step 1 if (str[0] == '-') { sign = -1; i++; } // Step 2 while (str[i] != '\0') { num = num * 10 + (str[i] - '0'); i++; } // Step 3 num *= sign; printf("%d\n", num); return 0; } Output-123 Convert String to Number in C++:1. Using Built-in Functions:Use the built-in function to convert the string to a number. C++ #include <iostream> #include <string> using namespace std; int main() { string str = "123"; // Step 1 int num = stoi(str); cout << num << endl; return 0; } Output123 2. String Concatenation:Check if the string represents a negative number and set a flag.Iterate over each character of the string and convert it to its corresponding numerical value.Calculate the numerical value using the digits. C++ #include <iostream> #include <string> using namespace std; int main() { string str = "-123"; int sign = 1, num = 0; // Step 1 if (str[0] == '-') { sign = -1; str = str.substr(1); } // Step 2 for (char c : str) { num = num * 10 + (c - '0'); } // Step 3 num *= sign; cout << num << endl; return 0; } Output-123 Convert String to Number in Java:1. Using Built-in Functions:Use the built-in function to convert the string to a number. Java public class Main { public static void main(String[] args) { String str = "123"; // Step 1 int num = Integer.parseInt(str); System.out.println(num); } } Output123 2. String Concatenation:Check if the string represents a negative number and set a flag.Iterate over each character of the string and convert it to its corresponding numerical value.Calculate the numerical value using the digits. Java public class Main { public static void main(String[] args) { String str = "-123"; int sign = 1, num = 0; // Step 1 if (str.charAt(0) == '-') { sign = -1; str = str.substring(1); } // Step 2 for (char c : str.toCharArray()) { num = num * 10 + (c - '0'); } // Step 3 num *= sign; System.out.println(num); } } Output-123 Convert String to Number in Python:1. Using Built-in Functions:Use the built-in function to convert the string to a number. Python str_num = "123" # Step 1 num = int(str_num) print(num) Output123 2. String Concatenation:Check if the string represents a negative number and set a flag.Iterate over each character of the string and convert it to its corresponding numerical value.Calculate the numerical value using the digits. Python str_num = "-123" sign = 1 num = 0 # Step 1 if str_num[0] == '-': sign = -1 str_num = str_num[1:] # Step 2 for c in str_num: num = num * 10 + int(c) # Step 3 num *= sign print(num) Output-123 Convert String to Number in C#:1. Using Built-in Functions:Use the built-in function to convert the string to a number. C# using System; class Program { static void Main() { string str = "123"; // Step 1 int num = int.Parse(str); Console.WriteLine(num); } } Output123 2. String Concatenation:Check if the string represents a negative number and set a flag.Iterate over each character of the string and convert it to its corresponding numerical value.Calculate the numerical value using the digits. C# using System; class Program { static void Main() { string str = "-123"; int sign = 1, num = 0; // Step 1 if (str[0] == '-') { sign = -1; str = str.Substring(1); } // Step 2 foreach (char c in str) { num = num * 10 + (c - '0'); } // Step 3 num *= sign; Console.WriteLine(num); } } Output-123 Convert String to Number in JavaScript:1. Using Built-in Functions:Use the built-in function to convert the string to a number. JavaScript let str_num = "123"; // Step 1 let num = parseInt(str_num); console.log(num); Output123 2. String Concatenation:Check if the string represents a negative number and set a flag.Iterate over each character of the string and convert it to its corresponding numerical value.Calculate the numerical value using the digits. JavaScript let str_num = "-123"; let sign = 1; let num = 0; // Step 1 if (str_num[0] === '-') { sign = -1; str_num = str_num.slice(1); } // Step 2 for (let c of str_num) { num = num * 10 + parseInt(c); } // Step 3 num *= sign; console.log(num); Output-123 Conclusion:Converting a string to a number involves interpreting the characters in the string as numerical values. This typically requires iterating through each character, converting it to its numeric equivalent, and considering any sign indicators. By following this process, strings can be transformed into their corresponding numeric representations, facilitating numerical operations and calculations in programming. Comment More infoAdvertise with us Next Article How to Convert String to Number C code_r Follow Improve Article Tags : C Language Similar Reads How to Convert Number to String Given a numerical value, convert it into a string representation. In this article, we will provide a detailed overview about different ways to convert Number to String in different languages. Table of Content Convert Number to String in CConvert Number to String in C++Convert Number to String in Jav 7 min read How to Convert String to Number in TypeScript? In TypeScript, converting a string to a number is a common operation that can be accomplished using several different methods. Each method offers unique advantages and can be chosen based on the specific requirements of your application. Below are the approaches to convert string to number in TypeSc 4 min read Converting Number to String in C++ In C++, converting integers to strings or converting numbers to strings or vice-versa is actually a big paradigm shift in itself. In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. Let's look at som 4 min read How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are 4 min read How to convert String to Float in PHP ? Converting a string to a float in PHP is a common requirement when handling numeric data stored in string format. This conversion allows the numeric string to be used in calculations or comparisons, ensuring accurate manipulation of data within the program.There are many methods to convert a string 3 min read String to Number Conversion in Julia Julia is a flexible, dynamic, and high-level programming language that can be used to write any application. Also, many of its features can be used for numerical analysis and computational science. Julia is being widely used in machine learning, visualization, and data science. Julia allows type con 3 min read How to Convert string to integer type in Golang? Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can 2 min read Words to Numbers Converter Tool in Python In this tutorial, we will guide you through the process of building a Word-to Numbers converter using Python. To enhance the user experience, we will employ the Tkinter library to create a simple and intuitive Graphical User Interface (GUI). This tool allows users to effortlessly convert words into 2 min read Swift - Convert String to Int Swift Swift provides a number of ways to convert a string value into an integer value. Though, we can convert a numeric string only into an integer value. In this article, we will see the two most common methods used to convert a string to an integer value. A string is a collection of characters. Swift pr 3 min read Like