Number Object Methods in TypeScript

The Number object have only the default methods that are present in any other object. Properties of Number object are explained in another article.
Below are some of the commonly used methods:
valueOf()
toString()
toPrecision()
toLocaleString()
toFixed()
toExponential()

toExponential()

This methods returns the exponential notation of the Number object in the string data type.
Syntax:

numberVariable.toExponential(numberOfDecimals?:number)

The parameter in this method is a number which specifies the number of decimal digits after decimal point. It is optional.
Below is an example:

var num1=123.456789;
var result=num1.toExponential();
console.log(result);
var result1=num1.toExponential(2);
console.log(result1);

Below is the output of above code:

toFixed()

This function is used to fix the the decimal digit in a fractional number. We pass the number of digits we want after decimal point as the parameter.
Syntax:

numberVariable.toFixed(numberOfDecimals?:number)

Below is an example:

var num1=123.4567;
console.log(num1.toFixed());
console.log(num1.toFixed(1));
console.log(num1.toFixed(5));

Below is the output of above code:

toLocaleString()

This method converts a number object into a human readable string representing the number using the locale of the environment.
Syntax:

numberVariable.toLocaleString();

Below is an example:

var num1=123.456;
var str=num1.toLocaleString();
console.log(num1.toLocaleString());
console.log("typeof str: " + typeof(str));

Below is the output of above code

toPrecision()

This method returns a string representing the number of digits to the mentioned precision. Parameter takes the number of digits for the precision and is optional.
Syntax

numberVariable.toPrecision(precisionNumber?:number)

Below is an example:

var num=12.3456;
console.log(num.toPrecision());
console.log(num.toPrecision(2));
console.log(num.toPrecision(4));
var str=num.toPrecision(2);
console.log(typeof(str));

Below is the output of above code

toString()

This method will convert a object into a string type. We can then use String methods on this object.
Syntax:

variable.toString()

Below is an example

var num=1234;
console.log(num.toString());
console.log(typeof(num.toString()));

Below is the output of above code:

valueOf()

Returns the primitive value of an object. This can be any object, number, string etc.
Syntax:

variable.valueOf();

Below is an example:

var num = new Number(100); 
console.log(num.valueOf());
var str = new String("Hello World!!"); 
console.log(str.valueOf());

Below is the output of above code:

Custom Methods and Parameters in C#

There are lots of in-built methods in C# that have their own functionality like Main() method, ToUpper(), String.Compare(string, string) etc. These methods can either return a value or not, or can have parameters or not. Methods are basically created for reuse, create once and we can use many times, anywhere in our program.

Basic syntax of a method in C# is:
returnType methodName(parameterList)
For example:

// The name of this method is AddNumbers which returns a value of type int and accepts two parameters, both of type int.
int AddNumbers(int num1, int num2)
{
     int sum = num1 + num2;
     return sum;
}
// The name of the method is SayHello. The return type of this method is void which means it does not return any value. 
// The parameter list is also empty, hence it does not accepts any parameter.
void SayHello()
{
    Console.WriteLine("Hellozz!");
}

Above are some of the ways you can create your own methods with/without parameters and with/without return values. The method names are normal variable names. We cannot have C# keywords as method names. Methods can have any number of parameters but can only return single value of any data type.

Calling/Invoking a Method
A method created is of no use until it is used anywhere in your program. A method can be used if it called somewhere in your program. Let’s use above created method in our program. Below is the sample code:

// In below class, there are three methods: Main() is the in-built method, AddNumbers() and SayHello() are custom methods.
class Program
{
        static void Main(string[] args)
        {
            // We are calling AddNumbers() method twice which indicate that we can reuse the methods.
            // We are passing two parameters of int type to AddNumbers() method which will return the sum of these two numbers.
            // We can use this returned value, in this case sum of two numbers, anywhere in our program.
            int sum = AddNumbers(20, 40);
            Console.WriteLine(sum);
            Console.WriteLine(AddNumbers(10, 34));
            SayHello();
        }
        static int AddNumbers(int num1, int num2)
        {
            int sum = num1 + num2;
            return sum;
        }
        static void SayHello()
        {
            Console.WriteLine("Hellozz!");
        } 
}

Below is the output of above code:

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started