Extracting numbers including 0 at the start of the number

1 ビュー (過去 30 日間)
Hubert 2022 年 11 月 24 日
コメント済み: Hubert 2022 年 11 月 30 日
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 :).

回答 (2 件)

Walter Roberson
Walter Roberson 2022 年 11 月 25 日
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 件のコメント
Hubert 2022 年 11 月 30 日
Thank you very much!
Told it to my teacher and he said that he didn't thought about this case in this task!


Voss 2022 年 11 月 24 日
編集済み: Voss 2022 年 11 月 24 日
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