Difference between %d and %f?

1.244 Ansichten (letzte 30 Tage)
xxtan1
xxtan1 am 3 Aug. 2020
Bearbeitet: Adam Danz am 4 Aug. 2020
Hi, I dont quite understand the difference between %d and %f and its usage in certain cases.
Does %d correspond to int in C and %f correspond to float?
What is the case I have to use %d instead of %f?

Akzeptierte Antwort

Adam Danz
Adam Danz am 3 Aug. 2020
Bearbeitet: Adam Danz am 4 Aug. 2020
Refer to the documentation.
%d represents signed integers (base 10).
sprintf('John is %d years old', 7)
% 'John is 7 years old'
%f represents floating point numbers.
sprintf('The first 8 dp of pi are %.8f', pi)
% 'The first 8 dp of pi are 3.14159265'
%f can also be used to represent integers by rounding floating point inputs. %d cannot be used in this way.
sprintf('Pi is rounded to %.0f', pi)
% 'Pi is rounded to 3'
sprintf('Pi is rounded to %.d', pi)
% 'Pi is rounded to 3e+00'

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by