Displaying a value using fprintf with certain number of decimals.

1 visualización (últimos 30 días)
I am trying to display the value of a using fprintf with only two decimals. Given a = 3.870081e+01;
Can you modify my code below to only display two decimals?
fprintf('The answer for a is %d m/s\n', a)
Thanks,

Respuesta aceptada

Walter Roberson
Walter Roberson el 5 de Mzo. de 2021
format long g
a = rand*10
a =
5.92737493941911
fprintf('The answer for a is %.2f m/s\n', a)
The answer for a is 5.93 m/s

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by