What is the proper format string for float?
18 ビュー (過去 30 日間)
古いコメントを表示
I would like to print a float without unnecessary characters. 0.2, 1.25, 0.02, but in case of lot of digits, I would like to cut the last digits: 1.234 to 1.23 %f, %.2f is not good I think
0 件のコメント
回答 (2 件)
Dasharath Gulvady
2015 年 5 月 29 日
If you are looking for a way to round off the decimal values, see the below thread:
Guillaume
2015 年 6 月 10 日
編集済み: Guillaume
2015 年 6 月 10 日
There is no formatting option that does exactly what you want. You could switch to using '%g' instead of '%f', this does not print trailing zeros, but the precision is then the number of significant digits instead of the number of digits after the decimal point. Furthermore '%g' switches to exponential notation if the magnitude of the number is too big or too small.
For the examples, you've given:
sprintf(n, '%.4g')
works
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!