Clear Filters
Clear Filters

convert double to string!

1,259 views (last 30 days)
fatemeh
fatemeh on 29 Dec 2013
Edited: Stephen23 on 5 Sep 2024 at 2:53
I want convert double number like 0.222833 to string but when i using num2str the number convert to '0.22283' and sixth digit of number is removed ,can u help me to convert it to string?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 29 Dec 2013
a=0.222833
a=sprintf('%.6f',a)
  4 Comments
Ron Fredericks
Ron Fredericks on 4 Sep 2024 at 22:52
Nice workaround. Thank you. Even using format('long') does not solve this number to 5-digit float to string conversion issue. Shouldn't this be marked as a bug - in code - or at least noted as a warning in docs related to "string"?
Stephen23
Stephen23 on 5 Sep 2024 at 2:44
Edited: Stephen23 on 5 Sep 2024 at 2:53
"Even using format('long') does not solve this number to 5-digit float to string conversion issue. Shouldn't this be marked as a bug ..."
Not a bug. The FORMAT function only controls how numbers are displayed in the command window, just as its documentation explicitly states "Numeric formats affect only how numbers appear in the display, not how MATLAB® computes or saves them." (bold added).
Text conversion function that are affected by the display FORMAT are very few and far between, the main one that comes to mind is quite appropriately
which is explicitly defined to capture the text displayed in the command window. Lets try it now:
format short
formattedDisplayText(pi)
ans =
" 3.1416 "
format long
formattedDisplayText(pi)
ans =
" 3.141592653589793 "
Note how (just as documented) this captures the entire display text, incuding newlines and other whitespace.
"...or at least noted as a warning in docs related to "string"?"
The STRING documentation states that it uses "Output format and precision equivalent to using num2str."
Taking a look at the NUM2STR documentation we see that it states that by default "The output format depends on the magnitudes of the original values." Nowhere in the NUM2STR documentatioon does it state that it uses the current command window display format, or make any reference to it at all. Lets compare:
format long
string(pi)
ans = "3.1416"
num2str(pi)
ans = '3.1416'
format short
string(pi)
ans = "3.1416"
num2str(pi)
ans = '3.1416'
Because the implicit conversion to STRING provides no way to control the numeric precision the STRING documentation already explictly advises "Use compose to specify more precise formatting." Lets try that:
compose("%.6f",pi)
ans = "3.141593"
So far everything is working exactly as documented and expected. It is unclear why you expect a warning for behaviors that are already documented.
To help beginners the function FORMAT should probably have been named DISPLAYFORMAT or something of that ilk, but that boat has long since sailed. The fashion for short command names is very much an indication of MATLAB's venerable age, back when programmers loved absurd cryptic commands that minimized typing on their 80-character terminals, nowadays programming fashion seems to have changed for longer, explicit command names.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!