How can I change the way MATLAB displays the significant digits of floating point numbers?
199 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 19 Nov. 2012
Bearbeitet: MathWorks Support Team
am 28 Mai 2021
How can I change the way MATLAB displays the significant digits of floating point numbers?
I want to adjust how the significant digits of floating point numbers are displayed in the MATLAB Command Window.
Akzeptierte Antwort
MathWorks Support Team
am 28 Mai 2021
Bearbeitet: MathWorks Support Team
am 28 Mai 2021
The FORMAT function allows users to set the default display method for floating point numbers in MATLAB.
The options for format are:
FORMAT Default. Same as SHORT.
FORMAT SHORT Scaled fixed point format with 5 digits.
FORMAT LONG Scaled fixed point format with 15 digits.
FORMAT SHORT E Floating point format with 5 digits.
FORMAT LONG E Floating point format with 15 digits.
FORMAT SHORT G Best of fixed or floating point format with 5 digits.
FORMAT LONG G Best of fixed or floating point format with 15 digits.
FORMAT HEX Hexadecimal format.
FORMAT + The symbols +, - and blank are printed for positive,
negative and zero elements. Imaginary parts are ignored.
FORMAT BANK Fixed format for dollars and cents.
FORMAT RAT Approximation by ratio of small integers.
Spacing:
FORMAT COMPACT Suppress extra line-feeds.
FORMAT LOOSE Puts the extra line-feeds back in.
For more information you should see the FORMAT documentation, accessible from MATLAB with the following command:
doc format
Here is a quick example:
>> format short
>> pi
ans =
3.1416
>> format long
>> pi
ans =
3.14159265358979
Currently, it is not possible to specify your own precision in MATLAB. A suggested work-around to this issue would be to use the CEIL and FLOOR functions, in combination with multiplication by magnitudes of 10, to round off to a particular precision.
For example, if you have a variable x = 3.1416, to obtain only three significant digits, you should do the following:
format short
x=pi
xtemp=x*1000
xtempt=ceil(xtemp)
x=xtempt/1000
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!