Using string() on double values without automatic rounding?

9 Ansichten (letzte 30 Tage)
Sondre
Sondre am 8 Mai 2023
Beantwortet: Stephen23 am 9 Mai 2023
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.

Akzeptierte Antwort

Matt J
Matt J am 8 Mai 2023
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 Kommentare
Matt J
Matt J am 8 Mai 2023
Bearbeitet: Matt J am 8 Mai 2023
You could use num2str,
string( num2str(123.123123123,'%.9f') )
ans = "123.123123123"

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 9 Mai 2023
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:

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by