In this article on Mechanicalland, we will show the number and integer types in Matlab®. Before starting to learn Matlab® coding, you need to know the integer types and how to show them in an intended way in Matlab®. There is a command in Matlab® to do it. So, you will see that the number formats display options in Matlab are very simple.
How to Display Different Number Formats in Matlab Language?
>> format long a=1.123165165165131215 a = 1.123165165165131 >>
If you want to show a number that has a very long decimal part on a long basis, you could type “format long” to show it in the first 16 characters at Command Window at Matlab as above.
>> a=11231651651651.31215834 a = 1.123165165165131e+13 >>
If you do not use the “format long” code to show a number, the same number will be shown as above. The scientific expression as shown above means 10^8 in Matlab.
>> format short a=1.312158346351 a = 1.3122 >>
Also, you can use “format short” code, as shown above, the long decimal number or long integers will be shown in 6 characters at Matlab Command Window.
>> a = 100063226060.32622236; fprintf("%1.10f",a) 100063226060.3262176514>>
If you have a number like “a” in Matlab, you can show this number’s intended decimals with the “fprintf” command as shown above. “1.10f” the 10f inside this code means, you want to show 10 strings after the decimal point of this number in the Matlab® Command Window.
In Matlab, you can also display the values and numbers in hexadecimal format. You need to use the code ‘format hex’. Check the example to understand how to do it.
format hex realmax ans = 7fefffffffffffff
In the example above, we typed the code of ‘format hex’. And we defined the realmax value in Matlab. So, you can see the answer in hexadecimal format.
Displaying Engineering Notations
Engineering notations are very important for us. So, we can display the numbers and values as engineering notations in Matlab. Also, you can make in in short engineering notation and long engineering notation.
You can do this with the format shortEng and longEng codes. To understand how it works in Matlab, check the examples below.
format shortEng x = 51.2 x = 5.1235e+000 format longEng x = 5.12345678900000e+000
Displaying Long Numbers as Simpler Ones
If you have an array that has different kinds of values in different ranges. Some of the basic and some of the complex. In Matlab, you can use the ‘format shortG’ code to display them in the most basic way. Check the code example in Matlab to see how it works.
x = [25 56.31156 255.52675 9876899999]; format shortG x = 25 56.312 255.53 9.8769e+09
As you see above, we defined an ‘x’ vector in Matlab. And then, we typed the code ‘format shortG’ and executed it. You can see the more basic notations of the elements of ‘x’.
Resetting to Default Format
You may have changed the format to different values. So, you can turn into the default format value in Matlab by using the ‘format default’ code. If you directly execute this code in Matlab, your format will turn into a default value.
Displaying Long and Short Scientific Display Format
You can also display the numbers in scientific formats in Matlab. You just need to use the ‘format shortE’ and ‘format longE’ codes. These codes will convert your values into scientific formats. Check the code examples to understand how it works again.
format shortE x = 9638573934 x = 9.6386e+09 format longE x = 9.638573934000000e+09
In the code example above, we typed the code ‘format shortE’. And then we executed this code. And we defined a variable ‘x’ with the value of 9638573934. So, the display of this variable is in the short scientific format as 9.6386e+09.
Also, we typed the code ‘format longE’. And then we called the variable ‘x’ again in Matlab. So, Matlab displays the value of ‘x’ in the as long scientific format as 9.638573934000000e+09.
Bank Format in Matlab
You can also show the currency format in Matlab. For example, you are using Matlab for banking or accounting purposes. You can type ‘format bank’ to display two digits after the decimal point. To understand how it works, check the code example below.
a = 3.4545 format bank a = 3.45
As you see above, the working principle of the bank format in Matlab is very simple. We defined a variable ‘a with the value of 3.4545. And then, we typed ‘format bank’. So, if we call variable ‘a’, Matlab will display it as 3.45.
Rational Format in Matlab
You can also show the integers as rational formats in Matlab. You can do this with the ‘format rational’ code. To understand it, check the code example below.
x = 0.5; format rational x = 1/2
In the code example above, we created a variable ‘x’ which has the value of 0.5. And we typed the code ‘format rational’. So, you can see that the variable is equal to the ‘1/2’ which is a rational value in Matlab.
The expression and displaying of numbers in Matlab are like that. You can see that the ‘format’ code is very useful in Matlab. So, you can display the numbers and variables in Matlab in different ways.
Do not forget to leave your comments and questions below about the number formats display options in Matlab below.
If you want further coding examples about displaying numbers in Matlab®, inform us in the comments.
This article is prepared for completely educative and informative purposes. Images used courtesy of Matlab®
Your precious feedbacks are very important to us.
Matlab provides other kinds of useful tools and functions. Check the other useful tools of Matlab below.