Using string() on double values without automatic rounding?

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 日

0 投票

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 件のコメント

Sondre
Sondre 2023 年 5 月 8 日
編集済み: Sondre 2023 年 5 月 8 日
I'm not sure im following? If I understand correctly, a double value can have 15 decimal places of precision. Even with numbers with less than 15 digits in total the problem is still there:
Meaning that the string() function rounds to four decimals. My question is wether it is possible to do this double->string conversion without this rounding.
I hope i have made myself more clear, sorry if my initial formulation was off.
Matt J
Matt J 2023 年 5 月 8 日
編集済み: Matt J 2023 年 5 月 8 日
You could use num2str,
string( num2str(123.123123123,'%.9f') )
ans = "123.123123123"
Sondre
Sondre 2023 年 5 月 8 日
Thanks!

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2023 年 5 月 9 日

2 投票

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:

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

リリース

R2022b

タグ

質問済み:

2023 年 5 月 8 日

回答済み:

2023 年 5 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by