convert double to string!

조회 수: 333 (최근 30일)
fatemeh
fatemeh 2013년 12월 29일
편집: Stephen23 2024년 9월 5일
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?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 29일
a=0.222833
a=sprintf('%.6f',a)
  댓글 수: 4
Ron Fredericks
Ron Fredericks 2024년 9월 4일
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 2024년 9월 5일
편집: Stephen23 2024년 9월 5일
"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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by