Filter löschen
Filter löschen

How to use str2double without rounding

74 Ansichten (letzte 30 Tage)
Aditya Deshmukh
Aditya Deshmukh am 20 Jan. 2017
Bearbeitet: Stephen23 am 20 Jan. 2017
Hi , I know this could be a possible duplicate but all those solutions stated there didn't work for me. I am using the str2double function and it always rounds the conversion to the 4 places after the decimal point. Do you guys have any idea on converting the whole string exactly the way it is without rounding ??
  1 Kommentar
Adam
Adam am 20 Jan. 2017
Bearbeitet: Adam am 20 Jan. 2017
It doesn't do this for me at all.
>> str2double( '4.34576543' )
ans =
4.34576543
It seems to round to the 14th decimal place when I try it:
>> str2double( '4.34576543197566529101' )
ans =
4.34576543197567

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Nut
Nut am 20 Jan. 2017
Hi,
maybe you need a proper set of the output display format:
https://it.mathworks.com/help/matlab/ref/format.html
The default format is "short", with the "long" format you can see more digits, look at the following example.
A = '0.219033048405430510873439814';
>> A
A =
0.219033048405430510873439814
>> str2double(A)
ans =
0.2190
>> format long
>> str2double(A)
ans =
0.219033048405431

Weitere Antworten (1)

Stephen23
Stephen23 am 20 Jan. 2017
Bearbeitet: Stephen23 am 20 Jan. 2017
str2double does not do any rounding: does its documentation mention "rounding" anywhere? In fact you are just getting confused by how numbers as displayed in MATLAB. How number values are displayed is a totally different thing to how they are stored in memory. str2double reads all of those digits correctly.
Use format to change how numbers are displayed:
>> format shortg
>> str2double('0.1234567890123456789')
ans = 0.12346
>> format longg
>> str2double('0.1234567890123456789')
ans = 0.123456789012346
There are plenty of format options: read the documentation and try some. Note that the double type is limited as to how many digits of precision it can handle. This is also not str2double's fault.

Kategorien

Mehr zu Data Type Conversion 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