Transform double to char with one or two decimal places
7 ビュー (過去 30 日間)
古いコメントを表示
I have a series of numbers:
n_A = 3.15;
n_B = 3.7;
I have to transform them into char considering 1 or 2 decimal digits based on the starting number, i.e. like this:
n_A = 3.15;
A = sprintf('%.2f',n_A);
n_B = 3.7;
B = sprintf('%.1f',n_B);
My question is how can I apply this:
sprintf('%.2f',#);
or this:
sprintf('%.1f',#);
knowing that the number has one or two digits after the decimal point?
0 件のコメント
回答 (1 件)
Stephen23
2023 年 9 月 11 日
編集済み: Stephen23
2023 年 9 月 11 日
You could use another format, e.g.:
fprintf('%.3g\n',[3.15,3.7])
2 件のコメント
Walter Roberson
2023 年 9 月 11 日
n_A = 3.15;
fprintf('%g\n', n_A);
n_B = 3.70;
fprintf('%g\n', n_B)
format long g
n_A
n_B
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!