Extracting numbers including 0 at the start of the number
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I've come to you with a question which I have a little trouble, I want to extract from number 02290234567 every single digit INCLUDING 0 on the start, so the result should be matrix 11 columns and 1 row x=[0 2 2 9 0 2 3 4 5 6 7]. I tried something like this
x=02290234567;
y=num2str(x);
i=1;
while (i<12)
x(i)=str2num(y(i))
i = i+1;
end
But it always forgets about 0 at the start! Please help :).
0 commentaires
Réponses (2)
Walter Roberson
le 25 Nov 2022
The way numbers like 02290234567 are stored internally is as IEEE 754 Binary Double Precision. It is a base 2 representation, and is basically a 53 bit integer divided by 2 to a power.
When it is time to display the floating point number to the user, the binary representation is converted to text, the usual convention is used: that leading zeros are not used except in the case where the absolute value of the number is less than 1.
The base-10 digits are never stored, and the number of leading zeros is never stored. MATLAB makes no attempt to distinguish between 2290234567 and 02290234567 and 002290234567 and 00000000000000000000000000000000000002290234567 in any way. It only stores the value associated with the number, and makes no attempt to store the representation.
If you need to be able to distinguish between 02290234567 and 002290234567 then you need to store the number as a character vector.
The exception is if you know ahead of time how many output digits it should be. In that case you can use
sprintf('%011d', 2290234567)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!