round (returning a number)

1 vue (au cours des 30 derniers jours)
Peter
Peter le 30 Août 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

Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 30 Août 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')

Plus de réponses (0)

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by