Keeping a zero after a decimal

Hi! I'm relatively new to MATLAB and am trying to round the values within this column vector to three significant digits. This has worked fine except for the second entry, which fails to keep a zero after the decimal point when rounded. If I have a vector: A = [27.372; 24.966; 28.908; 29.205; 23.783; 27.256], no matter whether I use: result = round(A, 3, 'significant') or result = round(A, 1), both produce the following: 27.4; 25; 28.9; 29.2; 23.8; 27.3. I would like the second row to be 25.0. Any help is much appreciated! Cheers :)

7 件のコメント

Stephen23
Stephen23 2017 年 12 月 6 日
編集済み: Stephen23 2023 年 2 月 14 日
"which fails to keep a zero after the decimal point when rounded"
Are you trying to display the values or do you want to perform some numeric operations on them?
Laya Rahbar Nikoukar
Laya Rahbar Nikoukar 2018 年 2 月 7 日
Hi! I have the same question as Andre Dixon. I want to display the values and the zeros after decimal should be displayed. Could you help me further?
Stephen23
Stephen23 2018 年 2 月 7 日
編集済み: Stephen23 2018 年 2 月 7 日
" I want to display the values and the zeros after decimal should be displayed. Could you help me further?"
Use sprintf, fprintf, num2str, etc, and specify the format to show as many decimal places you wish:
>> sprintf('%.2f',2.5)
ans = 2.50
Laya Rahbar Nikoukar
Laya Rahbar Nikoukar 2018 年 2 月 7 日
Thanks a lot!
per isakson
per isakson 2018 年 2 月 7 日
On R2016a
>> round( A, 3, 'significant')
ans =
27.4000
25.0000
28.9000
29.2000
23.8000
27.3000
>> round( A, 1 )
ans =
27.4000
25.0000
28.9000
29.2000
23.8000
27.3000
>>
format short controls the number of digits being displayed.
"I want to display the values" display where and by what mean?
Kurt
Kurt 2023 年 2 月 13 日
編集済み: Kurt 2023 年 2 月 13 日
A = [27.372; 24.966; 28.908; 29.205; 23.783; 27.256]
A = 6×1
27.3720 24.9660 28.9080 29.2050 23.7830 27.2560
display(sprintf('%0.1f\n',round(A,1)));
27.4 25.0 28.9 29.2 23.8 27.3
Stephen23
Stephen23 2023 年 2 月 14 日
@Kurt: SPRINTF() also performs rounding, so the separate ROUND() call is not required:
A = [27.372; 24.966; 28.908; 29.205; 23.783; 27.256];
fprintf('%0.1f\n',A)
27.4 25.0 28.9 29.2 23.8 27.3

サインインしてコメントする。

回答 (1 件)

Christopher Coello
Christopher Coello 2018 年 2 月 7 日
編集済み: Christopher Coello 2018 年 2 月 7 日

0 投票

Well not sure about the question. But if what you want is display the vector with trailing zero then you can use the undocumented sprintfc command
A = [27.372; 24.966; 28.908; 29.205; 23.783; 27.256];
% with one decimal digit
sprintfc('%0.1f',A)
% with two decimal digits
sprintfc('%0.2f',A)
% with five decimal digits
sprintfc('%0.5f',A)

6 件のコメント

Jos (10584)
Jos (10584) 2018 年 2 月 7 日
not undocumented anymore :D
Christopher Coello
Christopher Coello 2018 年 2 月 7 日
Is it ? I get
No results for sprintfc.
in Help search filed for R2017b
Jos (10584)
Jos (10584) 2018 年 2 月 7 日
O, sorry, it still is undocumented
Guillaume
Guillaume 2018 年 2 月 7 日
sprintfc has never been documented.
Since R2016b, it is also no longer needed. The documented compose function does the same thing better.
Christopher Coello
Christopher Coello 2018 年 2 月 7 日
Nice, I like compose ! Thanks Guillaume, I'll definitely drop sprintfc now
Elysi Cochin
Elysi Cochin 2022 年 10 月 14 日
Thanks @Guillaume

サインインしてコメントする。

タグ

質問済み:

2017 年 12 月 6 日

コメント済み:

2023 年 2 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by