Converting string to a double

18 views (last 30 days)
Bar Ben Amo
Bar Ben Amo on 24 Mar 2021
Commented: Fangjun Jiang on 24 Mar 2021
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 Comment
Stephen23
Stephen23 on 24 Mar 2021
Edited: Stephen23 on 24 Mar 2021
" 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.

Sign in to comment.

Answers (1)

Fangjun Jiang
Fangjun Jiang on 24 Mar 2021
Are you sure?
>> format long
>> a=str2double('1.7356922617E-3')
a =
0.001735692261700
  7 Comments
Walter Roberson
Walter Roberson on 24 Mar 2021
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
Fangjun Jiang
Fangjun Jiang on 24 Mar 2021
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'

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!