I have this problem that has been bothering me for quite a while.. I want to change the format of the number.. Don`t know how? Ive tried the help files but can't find the answer.. If you can help me please do..
Use the format command
Example:
format long; pi
3.141592653589793
format short e; pi
3.1416e+000
format short g; pi
3.1416
There are three different things you could potentially be referring to when you talk about the "format" of a number: the display format, the variable storage format in memory (i.e. the data type/class), and the storage format in a data file. I'll address each...
Display format: As already mentioned by Amro, the display format can be adjusted with the FORMAT command. However, this only affects how the numbers are displayed, not how they are stored in memory.
Variable types/classes: There are a number of numeric classes, both floating point and signed/unsigned integer types, that variables can use. By default, MATLAB variables are stored as double-precision floating point numbers. To convert to other types, you can use the variable type as a function to recast a value. For example,
a = uint8(0);
converts0
to an 8-bit unsigned integer type and stores it ina
, whileb = single(pi);
converts the valuepi
into single-precision and stores it inb
.File storage format: When reading/writing numeric values to files, the type of file affects how it will be stored. A binary file (written to and read from using FWRITE and FREAD) stores the complete binary representation of a number, and can thus represent it as exactly the same type as it is stored in memory as a variable (double-precision, single-precision, 8-bit integer, etc.).
Alternatively, a text file (written to and read from using FPRINTF and FSCANF, among other functions) will store a value in a string format that you must specify when outputting the values to the file. You can specify the digits of precision, as well as the format (such as exponential notation or hexadecimal). The documentation for FPRINTF specifies these output formats.
sprintf('%.2f', 1.4795e4);
(in particular: if you want it displayed/saved/printed a certain way, be explicit about it!)