Open In App

How to Convert Number to String

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Convert Number to String in C:

1. Using Built-in Functions:

  • Use the built-in function to convert the number to a string.
C
#include <stdio.h>

int main()
{
    int num = 123;
    char str[20];

    // Step 1
    sprintf(str, "%d", num);

    printf("%s\n", str);
    return 0;
}

Output
123

2. String Concatenation:

  • Check if the number is negative and set a flag.
  • Iterate over each digit of the number and convert it to its corresponding character representation.
  • Concatenate the characters to form the string.
C
#include <stdio.h>

int main()
{
    int num = 123;
    char str[20];
    int i = 0, sign = 0;

    // Step 1
    if (num < 0) {
        sign = 1;
        num = -num;
    }

    // Step 2
    do {
        str[i++] = num % 10 + '0';
        num /= 10;
    } while (num);

    if (sign) {
        str[i++] = '-';
    }

    str[i] = '\0';

    // Step 3
    for (int j = 0; j < i / 2; j++) {
        char temp = str[j];
        str[j] = str[i - j - 1];
        str[i - j - 1] = temp;
    }

    printf("%s\n", str);

    return 0;
}

Output
123

3. String Formatting:

  • Use string formatting functions or methods to convert a number to its string representation.
C
#include <stdio.h>

int main()
{
    int num = 3;
    char str[20];

    // Step 1
    sprintf(str, "%d", num);

    printf("%s\n", str);
    return 0;
}

Output
3

Convert Number to String in C++:

1. Using Built-in Functions:

  • Use the built-in function to convert the number to a string.
C++
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int num = 123;

    // Step 1
    string str = to_string(num);

    cout << str << endl;
    return 0;
}

Output
123

2. String Concatenation:

  • Check if the number is negative and set a flag.
  • Iterate over each digit of the number and convert it to its corresponding character representation.
  • Concatenate the characters to form the string.
C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int num = 123;
    string str;
    bool isNegative = false;

    // Step 1
    if (num < 0) {
        isNegative = true;
        num = -num;
    }

    // Step 2
    while (num > 0) {
        str += (char)('0' + num % 10);
        num /= 10;
    }

    if (isNegative) {
        str += '-';
    }

    // Step 3
    reverse(str.begin(), str.end());

    cout << str << endl;
    return 0;
}

Output
123

3. String Formatting:

  • Use string formatting functions or methods to convert a number to its string representation.
C++
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    int num = 3;
    stringstream ss;

    // Step 1
    ss << num;
    string str = ss.str();

    cout << str << endl;
    return 0;
}

Output
3

Convert Number to String in Java:

1. Using Built-in Functions:

  • Use the built-in function to convert the number to a string.
Java
public class Main {
    public static void main(String[] args)
    {
        int num = 123;

        // Step 1
        String str = Integer.toString(num);

        System.out.println(str);
    }
}

Output
123

2. String Concatenation:

  • Check if the number is negative and set a flag.
  • Iterate over each digit of the number and convert it to its corresponding character representation.
  • Concatenate the characters to form the string.
Java
public class Main {
    public static void main(String[] args)
    {
        int num = 123;
        StringBuilder str = new StringBuilder();
        boolean isNegative = false;

        // Step 1
        if (num < 0) {
            isNegative = true;
            num = -num;
        }

        // Step 2
        while (num > 0) {
            str.append((char)('0' + num % 10));
            num /= 10;
        }

        if (isNegative) {
            str.append('-');
        }

        // Step 3
        System.out.println(str.reverse().toString());
    }
}

Output
123

3. String Formatting:

  • Use string formatting functions or methods to convert a number to its string representation.
Java
public class Main {
    public static void main(String[] args)
    {
        int num = 3;

        // Step 1
        String str = String.format("%d", num);

        System.out.println(str);
    }
}

Output
3

Convert Number to String in Python:

1. Using Built-in Functions:

  • Use the built-in function to convert the number to a string.
Python
num = 123

# Step 1
result = str(num)

print(result)

Output
123

2. String Concatenation:

  • Check if the number is negative and set a flag.
  • Iterate over each digit of the number and convert it to its corresponding character representation.
  • Concatenate the characters to form the string.
Python
num = 123

# Step 1
if num < 0:
    sign = "-"
    num = -num
else:
    sign = ""

# Step 2
digits = []
while num > 0:
    digits.append(chr(ord('0') + num % 10))
    num //= 10

# Step 3
result = sign + ''.join(reversed(digits))

print(result)

Output
123

3. String Formatting:

  • Use string formatting functions or methods to convert a number to its string representation.
Python
num = 3

# Step 1
result = "{:d}".format(num)

print(result)

Output
3

Convert Number to String in C#:

1. Using Built-in Functions:

  • Use the built-in function to convert the number to a string.
C#
using System;

class Program
{
    static void Main()
    {
        int num = 123;

        // Step 1
        string str = num.ToString();

        Console.WriteLine(str);
    }
}

Output
123

2. String Concatenation:

  • Check if the number is negative and set a flag.
  • Iterate over each digit of the number and convert it to its corresponding character representation.
  • Concatenate the characters to form the string.
C#
using System;

class Program
{
    static void Main()
    {
        int num = 123;
        string str = "";
        bool isNegative = false;

        // Step 1
        if (num < 0)
        {
            isNegative = true;
            num = -num;
        }

        // Step 2
        while (num > 0)
        {
            str += (char)('0' + num % 10);
            num /= 10;
        }

        if (isNegative)
        {
            str += '-';
        }

        // Step 3
        char[] charArray = str.ToCharArray();
        Array.Reverse(charArray);
        str = new string(charArray);

        Console.WriteLine(str);
    }
}

Output
123

3. String Formatting:

  • Use string formatting functions or methods to convert a number to its string representation.
C#
using System;

class Program
{
    static void Main()
    {
        int num = 3;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        // Step 1
        sb.Append(num);
        string str = sb.ToString();

        Console.WriteLine(str);
    }
}

Output
3

Convert Number to String in Javascript:

1. Using Built-in Functions:

  • Use the built-in function to convert the number to a string.
JavaScript
let num = 123;

// Step 1
let result = num.toString();

console.log(result);

Output
123

2. String Concatenation:

  • Check if the number is negative and set a flag.
  • Iterate over each digit of the number and convert it to its corresponding character representation.
  • Concatenate the characters to form the string.
JavaScript
let num = 123;
let isNegative = false;

// Step 1
if (num < 0) {
    isNegative = true;
    num = -num;
}

let str = '';

// Step 2
while (num > 0) {
    str = String.fromCharCode('0'.charCodeAt(0) + num % 10) + str;
    num = Math.floor(num / 10);
}

if (isNegative) {
    str = '-' + str;
}

// Step 3
console.log(str);

Output
123

3. String Formatting:

  • Use string formatting functions or methods to convert a number to its string representation.
JavaScript
let num = 3;

// Step 1
let result = num.toString();

console.log(result);

Output
3

Conclusion:

Converting a number to a string generally involves using language-provided functions or manual techniques. Built-in functions like ToString() or to_string() are commonly used for this purpose in many programming languages.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads