Effacer les filtres
Effacer les filtres

How can we convert a number into a string value with the exact number of digits present in the number?

30 vues (au cours des 30 derniers jours)
number=0.0199155
number =
0.0199155
When I perform the following operation, it cuts off the last digit.
string=num2str(number)
string =
'0.019915'
I want it to be exactly '0.0199155', and not even '0.019915500000000'.
Please can someone help me in this case, as I have a lot of folders with different numbers and precision and later I want to read each folder name, so I need the strings to match exactly the numbers. Thank You!
  2 commentaires
Stephen23
Stephen23 le 20 Mai 2022
"I have a lot of folders with different numbers and precision and later I want to read each folder name, so I need the strings to match exactly the numbers."
Your approach is fragile. Why not just use DIR?
Muhammad Ahsan Khan
Muhammad Ahsan Khan le 20 Mai 2022
Modifié(e) : Muhammad Ahsan Khan le 20 Mai 2022
I did actually... I read all the folders in my directry (they all are time folders) and then saved them in an array of cells...
Then I converted the cells to double, and then, to read the folders in a For loop, I needed to convert the double into string so that I could concatenate the path of the directories one by one...

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 20 Mai 2022
Modifié(e) : Stephen23 le 20 Mai 2022
sprintf('%.15g',0.0199155)
ans = '0.0199155'
sprintf('%.15g',0.01991556789)
ans = '0.01991556789'
sprintf('%.15g',23456.7891234)
ans = '23456.7891234'

Plus de réponses (2)

adeq123
adeq123 le 20 Mai 2022
Hi,
You converted it correctly and you store the whole number in the workspace. However, you need to change the display format to see the whole number in the Command Window. Try this one
x = '9.876543218';
str2double(x);
format long;
z
  2 commentaires
Muhammad Ahsan Khan
Muhammad Ahsan Khan le 20 Mai 2022
Yeah this I figured out. Thank you so very much.
However, I posted a wrong question before (really sorry for that). Now I have corrected the questtion and it is the other way around. I need to convert a numeric value to string with all the digits present in the number.
num2str cuts off the digits from 6 digits after the decimal point.
adeq123
adeq123 le 20 Mai 2022
Similar story though.
Try this one:
number = 0.0199155;
string=num2str(number, 10)

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 20 Mai 2022
The digits "actually present" in the number assigned are 0.0199154999999999991755483819133587530814111232757568359375
The number you are thinking of 0.0199155 cannot be exactly represented in any finite binary floating point number. It is a mathematical impossibility, for the same reason that you cannot represent exactly 1/3 in any finite decimal expansion. 0.333...333 * 3 = 0.999...999 not 1 exactly no matter how many digits you use, and for the same mathematical reason 1/10 cannot be exactly represented as a finite binary fraction.
If you need exactly 199155/10000000 represented then you will need to to switch to something like the Symbolic Toolbox.
Or you could make the string form the fundamental form and str2double when you need the numeric approximation.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by