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

3 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2020-8-21
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by