Converting string to a double

Hello everyone.
i'm programming a software that gets different values from a txt file, and plotting a graph based on these values.
In order to create the graph I had to convert the strings in the txt file to a double, but the str2double function rounds the values so the graph isn't accurate.
for example, the string to convert is: 1.7356922617E-3, and the doublue value after applying the method is 1.73569E-3.
is there any method that convert without rounding?
thank you.

1 个评论

" but the str2double function rounds the values so the graph isn't accurate."
I very much doubt that str2double "rounds the values", what is much more likely is that you are confusing how numeric data are displayed with how numeric data are stored, which are two completely different things.
You can easily change how the data are displayed:
format short
str = '1.7356922617E-3';
val = str2double(str)
val = 0.0017
format long
val
val =
0.001735692261700
So str2double has not "rounded" the value at all, all of the digits are converted and stored as expected.

请先登录,再进行评论。

回答(1 个)

Are you sure?
>> format long
>> a=str2double('1.7356922617E-3')
a =
0.001735692261700

7 个评论

Exactly. The values are not rounded, but just the display in the command window.
Note, there is a limit on the precision. I believe it is 15 digits of precision. Need to find it somewhere in the doc.
>> b=str2double('100.12345678901234567')
b =
1.001234567890124e+02
Stephen23
Stephen23 2021-3-24
编辑:Stephen23 2021-3-24
"I believe it is 15 digits of precision"
The "precision" is more complex than that, it really depends what you are doing with the values:
In MATLAB, it seems to be 15 digits after the decimal point.
format long
c=str2double('10.12345678901234567890')
c=str2double('1.12345678901234567890')
c=str2double('0.12345678901234567890')
c =
10.123456789012346
c =
1.123456789012346
c =
0.123456789012346
"In MATLAB, it seems to be 15 digits after the decimal point."
Your examples only demonstrate the display format you selected (which has 15 digits after the decimal point)
Your examples do not demonstrate any "limit" on the conversion precision.
c=str2double('10.12345678901234567890')
c = 10.1235
fprintf('%.999g\n', c)
10.1234567890123461353368838899768888950347900390625
This has apparently remembered 16 total digits correctly, 10.12345678901234. Is that really the case?
fprintf('%.999g\n', 10.12345678901234567890) %do we get the same?
10.1234567890123461353368838899768888950347900390625
fprintf('%.999g\n', 10.123456789012340)
10.123456789012340806266365689225494861602783203125
fprintf('%.999g\n', 10.123456789012344)
10.12345678901234435898004448972642421722412109375
fprintf('%.999g\n', 10.123456789012345)
10.12345678901234435898004448972642421722412109375
fprintf('%.999g\n', 10.123456789012346)
10.1234567890123461353368838899768888950347900390625
fprintf('%.999g\n', 10.123456789012350)
10.1234567890123496880505626904778182506561279296875
fprintf('%.999g\n', 10.12345678901234567890*(1-eps))
10.12345678901234435898004448972642421722412109375
fprintf('%.999g\n', 10.12345678901234567890*(1+eps))
10.123456789012347911693723290227353572845458984375
So after 10.12345678901234 it is taking into account that what follows is something on the high side of 5x in order to decide whether to round down to 10.123456789012344 or up to 10.123456789012346
good points. Now I understand better. It certainly can read in more than 15 digits after decimal point.
>> p1=sprintf('%40.30f',pi)
p1 =
' 3.141592653589793115997963468544'
>> d=str2double(p1)
d =
3.141592653589793
>> p2=sprintf('%40.30f',d)
p2 =
' 3.141592653589793115997963468544'

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by