round (returning a number)

2 visualizaciones (últimos 30 días)
Peter
Peter el 30 de Ag. de 2011
i have a value: a = 1.50000 and i want to round it to 1.5 as a number
i use: a = num2str(a, '%7.1')
now: a = 1.5 but it is a string
so i use: a = str2num(a)
but now: a = 1.50000
Argghhh

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 30 de Ag. de 2011
What do you mean round in this case? If a=1.50123 and you want a=1.5, that is rounding. If a=1.50000, and you want a=1.5, that's just visual.
>> a=1.5
a =
1.5000
>> format short g
>> a
a =
1.5
If you want to put it in a uitable for example, the column format of "short g" still has 4 digits of precision, which means 1.5000. So it looks like you have to first make it string format like you did. To put it in a table anywhere else, you can use num2str() or fprintf('%7.1')

Más respuestas (0)

Categorías

Más información sobre Characters and Strings 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