round (returning a number)

2 views (last 30 days)
Peter
Peter on 30 Aug 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

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 30 Aug 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')

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!