How to Convert String to Number - GeeksforGeeks (2024)

Last Updated : 28 Mar, 2024

Improve

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 C
  • Convert String to Number in C++
  • Convert String to Number in Java
  • Convert String to Number in Python
  • Convert String to Number in C#
  • Convert String to Number in JavaScript

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 <stdio.h>#include <stdlib.h>int main(){ char str[] = "123"; // Step 1 int num = atoi(str); printf("%d\n", num); return 0;}

Output

123

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;}

Output

123

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); }}

Output

123

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 1num = int(str_num)print(num)

Output

123

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 = 1num = 0# Step 1if str_num[0] == '-': sign = -1 str_num = str_num[1:]# Step 2for c in str_num: num = num * 10 + int(c)# Step 3num *= signprint(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); }}

Output

123

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 1let num = parseInt(str_num);console.log(num);

Output

123

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 1if (str_num[0] === '-') { sign = -1; str_num = str_num.slice(1);}// Step 2for (let c of str_num) { num = num * 10 + parseInt(c);}// Step 3num *= 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.



Like Article

Suggest improvement

Previous

How to Convert Number to String

Next

How to convert string to number in TypeScript ?

Share your thoughts in the comments

Please Login to comment...

How to Convert String to Number - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 5688

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.