How do I change string to double without changing number of zero under decimal point?

2 Ansichten (letzte 30 Tage)
a = '24.5';aa = "24.5";
b = str2double(aa)
d = double(a)
d = double(aa)
c = cast(aa,'double')
c = cast(a,'double')
i know
class(a)
class(aa)
answer is
ans = 'char'
ans = 'string'
Each answer shows either
24.5000
or
[50 52 46 53]
Question is that how can I change class char to double without changing number of zero under decimal point?
24.5
Is there any function?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Aug. 2020
You cannot. class double does not keep track of how many trailing decimal points are present. class double is not stored in decimal at all: it is stored in binary floating point using an international standard representation that cannot exactly represent 1/10 (for the same mathematical reasons that decimal cannot exactly represent 1/3)
When you are seeing 2.5000 you are seeing one of the several different ways that the mathematical number can be represented. If you were to command
format long g
and display the number again you would see another representation. format bank would give you another.

Weitere Antworten (0)

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by