franciso
franciso

Reputation: 41

How do I go from 1.4795e+004 to 14795.00?

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..

Upvotes: 4

Views: 8791

Answers (3)

Jason S
Jason S

Reputation: 189626

sprintf('%.2f', 1.4795e4);

(in particular: if you want it displayed/saved/printed a certain way, be explicit about it!)

Upvotes: 0

gnovice
gnovice

Reputation: 125854

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); converts 0 to an 8-bit unsigned integer type and stores it in a, while b = single(pi); converts the value pi into single-precision and stores it in b.

  • 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.

Upvotes: 8

Amro
Amro

Reputation: 124553

Use the format command

Example:

format long; pi
   3.141592653589793
format short e; pi
   3.1416e+000
format short g; pi
   3.1416

Upvotes: 6

Related Questions