Using string() on double values without automatic rounding?

조회 수: 10 (최근 30일)
Sondre
Sondre 2023년 5월 8일
답변: Stephen23 2023년 5월 9일
Hello,
When i use string() with double values, the number is converted to a string where the number is rounded to four decimals, even when there are more decimals in the original double value. Image:
I am currently writing a conversion script where i need the extra decimals. Is it possible to disable this automatic rounding?
Thank you.

채택된 답변

Matt J
Matt J 2023년 5월 8일
even when there are more decimals in the original double value
There are no more decimals in the original double value. A double value can only have 15 digits of precision.
  댓글 수: 3
Matt J
Matt J 2023년 5월 8일
편집: Matt J 2023년 5월 8일
You could use num2str,
string( num2str(123.123123123,'%.9f') )
ans = "123.123123123"

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

추가 답변 (1개)

Stephen23
Stephen23 2023년 5월 9일
Rather than slow NUM2STR and then STRING, for a scalar the simpler and more efficient approach is to just call SPRINTF:
sprintf("%.9f",123.123123123)
ans = "123.123123123"
For non-scalar values use COMPOSE:

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by