using fprintf to print same length values

4 ビュー (過去 30 日間)
Ronaldo
Ronaldo 2013 年 12 月 30 日
編集済み: Daniel 2017 年 9 月 1 日
How can I use fprintf to print values with the same length (the number of digits after the pointer is changing based on the number of digits before the pointer)?
0.00000000
30.0000248
360.000305

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 30 日
編集済み: Azzi Abdelmalek 2013 年 12 月 30 日
b=360.000305
m=9 % fixed length
out=sprintf('%9.9f',b)
ii=numel(strfind(out,'.'))
out=out(1:m+ii)
  8 件のコメント
Ronaldo
Ronaldo 2013 年 12 月 31 日
Please add the following line (for the sake of completeness). I will accept the answer. Thanks for your great help.
fprintf('%s\n', out);
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 31 日
Ronaldo, I am not adding anything.

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


Daniel
Daniel 2017 年 9 月 1 日
編集済み: Daniel 2017 年 9 月 1 日
b=360.000305
m=9 % fixed length
out=sprintf('%9.9f',b)
ii=numel(strfind(out,'.'))
out=out(1:m+ii)
% Azzi's got no time for correctness
fprintf('%s\n', out)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by