How to Convert String to Number
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:
1. Using Built-in Functions:
- Use the built-in function to convert the string to a number.
#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.
#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.
#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.
#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.
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.
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.
str_num = "123"
# Step 1
num = 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.
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.
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.
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.
let str_num = "123";
// Step 1
let 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.
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.