フィルターのクリア

removing trailing zeros at the end of floating number

36 ビュー (過去 30 日間)
Abhijit Sardar
Abhijit Sardar 2021 年 4 月 21 日
回答済み: Adam Danz 2021 年 4 月 21 日
I m getting the numerical result with trailing zeros even after rounding of to few decimal places for eg.:- 3.265000000000 how do i remove those zeroes and just get the value as 3.265

回答 (2 件)

DGM
DGM 2021 年 4 月 21 日
編集済み: DGM 2021 年 4 月 21 日
Assuming this is for display purposes, try something like
fprintf('%6.3f\n',3.2653246546516)
gives
3.265
  3 件のコメント
James Tursa
James Tursa 2021 年 4 月 21 日
編集済み: James Tursa 2021 年 4 月 21 日
The trailing 0's is an artifact of converting the binary floating point bit pattern into a decimal number for display purposes, so this is just a display issue. The number isn't stored in decimal so those decimal 0's you are worried about don't exist in the stored number. The stored number is using IEEE binary floating point format, not any decimal format. What are you doing with these numbers downstream in your code where you think these trailing 0's will cause you a problem?
Also, many decimal numbers can't be represented exactly in binary floating point anyway. So if you were to print out many more digits, there will be non-zero digits after some trailing 0's in the decimal conversion. This is the nature of using binary floating point for calculations.
Abhijit Sardar
Abhijit Sardar 2021 年 4 月 21 日
but when i am copy pasting the data in text file trailing zeroes are still there

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


Adam Danz
Adam Danz 2021 年 4 月 21 日
format long
x = -3.265000000000;
str = sprintf('%g\n',x)
str =
'-3.265 '
x = -3.2650000000001;
str = sprintf('%g\n',x)
str =
'-3.265 '

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by