MATLAB Converting Double to String: Preserving the Last Digit

  • Thread starter Thread starter NoobixCube
  • Start date Start date
  • Tags Tags
    String
AI Thread Summary
When converting a double to a string in MATLAB, the default behavior of the num2str function can truncate trailing zeros, as seen in the example with the number 546.790, which results in the string "546.79". To retain the trailing zero, users can specify a format for the conversion, such as using '%3.3f' to maintain the desired precision. Additionally, other formatting options, including scientific notation, are available and can be found in the Help manual for num2str, which provides a comprehensive list of formats.
NoobixCube
Messages
154
Reaction score
0
Hi all,
I am converting a double to a string. But in the following case:
-----------
format long
numnum=546.790;
num=num2str(numnum)
length(num)
---------------
when 'numnum' is converted to a string, the command truncates the number by leaving off the last digit, the zero. Is there a way to keep the zero on there when converting to a string?
 
Physics news on Phys.org
Yes, specify the format as a floating point number
num2str(numnum,'%3.3f')
or use one of the other formats (scientific notation, etc.) listed in the Help manual. Check the Help entry for num2str and you'll see a clickable link to the format list.
 
Back
Top