How to make num2str output with 4 digits left of decimal? eg 0-> 0000

59 ビュー (過去 30 日間)
Aaron Greenbaum
Aaron Greenbaum 2017 年 4 月 7 日
コメント済み: Walter Roberson 2017 年 4 月 7 日
I am trying to get num2str to fomat its output with 4 digits. For example num2str(0) -> '0000' or num2str(125)-> '0125'
I know that there is a way to do this for digits after the decimal but I'm not sure how to do it for digits before the decimal.

採用された回答

Aaron Greenbaum
Aaron Greenbaum 2017 年 4 月 7 日
編集済み: Aaron Greenbaum 2017 年 4 月 7 日
you can format num2str using the following modifier to get the output I desired. num2str(125, '%04.f') -> '0125'
without the . after the 4 you get 125.0

その他の回答 (1 件)

the cyclist
the cyclist 2017 年 4 月 7 日
編集済み: the cyclist 2017 年 4 月 7 日
I suggest that instead you use the sprintf command for this. For example,
sprintf('%04d',125)
or
sprintf('%04d',0)
The formatting specification is pretty versatile, but can take a little while to become facile with.

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by