removing trailing zeros at the end of floating number

54 views (last 30 days)
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

Answers (2)

DGM
DGM on 21 Apr 2021
Edited: DGM on 21 Apr 2021
Assuming this is for display purposes, try something like
fprintf('%6.3f\n',3.2653246546516)
gives
3.265
  3 Comments
Abhijit Sardar
Abhijit Sardar on 21 Apr 2021
but when i am copy pasting the data in text file trailing zeroes are still there

Sign in to comment.


Adam Danz
Adam Danz on 21 Apr 2021
format long
x = -3.265000000000;
str = sprintf('%g\n',x)
str =
'-3.265 '
x = -3.2650000000001;
str = sprintf('%g\n',x)
str =
'-3.265 '

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!