how could numbers be displayed only in 4 digits (also in answer of sprintf)

23 ビュー (過去 30 日間)
mohammad
mohammad 2011 年 9 月 22 日
Does anyone know how could numbers be displayed only in 4 digits (also in answer of sprintf) [not more than 4)
a=3.78676789728978978978978789
sprintf('%d',a)= 3.786768e+000 % but i dont need this format
sprintf('%f',a)= 3.786768 % this is better but only 4 digits is needed
I need: 3.7867 or 3.786

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 22 日
sprintf('%4.3f',pi)
doc sprintf

その他の回答 (1 件)

topdawgnate
topdawgnate 2011 年 9 月 22 日
Check out the sprintf help page
and then modify your code to this
sprintf('%0.4f',a)
-Nate

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by