フィルターのクリア

How can I display values to one decimal place?

120 ビュー (過去 30 日間)
Steffen Adria
Steffen Adria 2011 年 7 月 12 日
編集済み: Walter Roberson 2020 年 6 月 8 日
Is it possible to display values to only one decimal place?
Thanks
Edit: I'm trying to do it for a UI table

回答 (3 件)

Sean de Wolski
Sean de Wolski 2011 年 7 月 12 日
Use sprintf
sprintf('%1.1f',pi)
  2 件のコメント
abdul rehman
abdul rehman 2020 年 6 月 7 日
why the following code giving wrong answer
Walter Roberson
Walter Roberson 2020 年 6 月 8 日
編集済み: Walter Roberson 2020 年 6 月 8 日
If you are referring to %10.1f as suggested by Oleg, then a difficulty with it is it is an attempt to create aligned columns, but uitable usually default to proportional spaced fonts that are not going to line up.
Using %10.1f could potentially look okay if you forced the font to be one of the monospaced fonts.
Note: when you use sprintf() to format values for uitable, you would save the result of sprintf() into a cell array of character vectors, and that cell array would be the Data that you would set for the uitable. The resulting entry would be character, not numeric, so if you permit editing of the column the user could easily put in non-numbers and whatever they enter is not going to be automatically aligned (or have its display rounded to the desired number of decimal places.)

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


Oleg Komarov
Oleg Komarov 2011 年 7 月 12 日
sprintf('%10.1f',10.231214321)

Walter Roberson
Walter Roberson 2011 年 7 月 12 日
Not without overriding the display() method. In particular, it is not possible to do this using the format() command.
If you have special formatting requirements it is usually best to use fprintf() or sprintf()
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 7 月 12 日
Sorry, uitable cannot specify the number of decimal places. The closest you can get to that is to specify "bank" format for the column, which would get you two decimal places.
To display data to one decimal place in a uitable, create the table column as char and write pre-formatted strings in to the cells.

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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by