Extracting numbers including 0 at the start of the number

1 view (last 30 days)
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
while (i<12)
i = i+1;
But it always forgets about 0 at the start! Please help :).

Answers (2)

Walter Roberson
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'
  1 Comment
Hubert on 30 Nov 2022
Thank you very much!
Told it to my teacher and he said that he didn't thought about this case in this task!

Sign in to comment.

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

Community Treasure Hunt

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

Start Hunting!

Translated by