# Extracting numbers including 0 at the start of the number

1 view (last 30 days)
Hubert on 24 Nov 2022
Commented: Hubert on 30 Nov 2022
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

Walter Roberson on 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)
ans = '02290234567'
Hubert on 30 Nov 2022
Thank you very much!

Voss on 24 Nov 2022
Edited: Voss on 24 Nov 2022
x = '02290234567';
x = x - '0'
x = 1×11
0 2 2 9 0 2 3 4 5 6 7