Skip to main content

Convert number to string

Converting a number to a string means converting a numerical value to a string of characters, where the characters represent the digits of the number. For example, if you have the number 123, converting it to a string would result in the character string "123". This conversion is often necessary in programming when you need to display or manipulate numbers as text, or when you need to concatenate numbers with other strings.

Program Solution

/*
  Java Program for 
  Convert number to string 
*/
public class Convertion
{
    public void numberToString(int number)
    {
        // Convert number to string
        String num = Integer.toString(number);
        // Display the converted string
        System.out.println(num);
    }
    public static void main(String[] args)
    {
        Convertion task = new Convertion();
        // Test Cases
        task.numberToString(123);
        task.numberToString(-123);
        
    }
}

input

123
-123
// Include header file
#include <iostream>

#include <string>

using namespace std;
/*
  C++ Program for 
  Convert number to string 
*/
class Convertion
{
	public: void numberToString(int number)
	{
		// Convert number to string
		string num = to_string(number);
		// Display the converted string
		cout << num << endl;
	}
};
int main()
{
	Convertion *task = new Convertion();
	// Test Cases
	task->numberToString(123);
	task->numberToString(-123);
	return 0;
}

input

123
-123
// Include namespace system
using System;
/*
  Csharp Program for 
  Convert number to string 
*/
public class Convertion
{
	public void numberToString(int number)
	{
		// Convert number to string
		String num = number.ToString();
		// Display the converted string
		Console.WriteLine(num);
	}
	public static void Main(String[] args)
	{
		Convertion task = new Convertion();
		// Test Cases
		task.numberToString(123);
		task.numberToString(-123);
	}
}

input

123
-123
<?php
/*
  Php Program for 
  Convert number to string 
*/
class Convertion
{
	public	function numberToString($number)
	{
		// Convert number to string
		$num = strval($number);
		// Display the converted string
		echo $num."\n";
	}
}

function main()
{
	$task = new Convertion();
	// Test Cases
	$task->numberToString(123);
	$task->numberToString(-123);
}
main();

input

123
-123
/*
  Node JS Program for 
  Convert number to string 
*/
class Convertion
{
	numberToString(number)
	{
		// Convert number to string
		var num = number.toString();
		// Display the converted string
		console.log(num);
	}
}

function main()
{
	var task = new Convertion();
	// Test Cases
	task.numberToString(123);
	task.numberToString(-123);
}
main();

input

123
-123
#  Python 3 Program for 
#  Convert number to string 
class Convertion :
	def numberToString(self, number) :
		num = str(number)
		#  Display the converted string
		print(num)
	

def main() :
	task = Convertion()
	#  Test Cases
	task.numberToString(123)
	task.numberToString(-123)

if __name__ == "__main__": main()

input

123
-123
#  Ruby Program for 
#  Convert number to string 
class Convertion 
	def numberToString(number) 
		#  Convert number to string
		num = number.to_s
		#  Display the converted string
		print(num, "\n")
	end

end

def main() 
	task = Convertion.new()
	#  Test Cases
	task.numberToString(123)
	task.numberToString(-123)
end

main()

input

123
-123
/*
  Scala Program for 
  Convert number to string 
*/
class Convertion()
{
	def numberToString(number: Int): Unit = {
		// Convert number to string
		var num: String = number.toString;
		// Display the converted string
		println(num);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Convertion = new Convertion();
		// Test Cases
		task.numberToString(123);
		task.numberToString(-123);
	}
}

input

123
-123
/*
  Swift 4 Program for 
  Convert number to string 
*/
class Convertion
{
	func numberToString(_ number: Int)
	{
		// Convert number to string
		let num: String = String(number);
		// Display the converted string
		print(num);
	}
}
func main()
{
	let task: Convertion = Convertion();
	// Test Cases
	task.numberToString(123);
	task.numberToString(-123);
}
main();

input

123
-123
/*
  Kotlin Program for 
  Convert number to string 
*/
class Convertion
{
	fun numberToString(number: Int): Unit
	{
		// Convert number to string
		val num: String = number.toString();
		// Display the converted string
		println(num);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Convertion = Convertion();
	// Test Cases
	task.numberToString(123);
	task.numberToString(-123);
}

input

123
-123




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment