Add 0 to the front of a number.

5 views (last 30 days)
function Output = dial(Input)
c = upper(Input);
j(length(c)) = 0;
a = 1;
for k = c
switch k
case '0' '
j(a) = 0;
case '1'
j(a) = 1;
case {'A','B','C','2'}
j(a) = 2;
case {'D','E','F','3'}
j(a) = 3;
case {'G','H','I','4'}
j(a) = 4;
case {'J','K','L','5'}
j(a) = 5;
case {'M','N','O','6'}
j(a) = 6;
case {'P','Q','R','S','7'}
j(a) = 7;
case {'T','U','V','8'}
j(a) = 8;
case {'W','X','Y','Z','9'}
j(a) = 9;
otherwise
j(a) = 0;
end
a=a+1;
end
Output = uint64(polyval(j,10));
end
Here at the end I get a numerical array in j but the first element in j is 0. Now when I convert j to a single number and store it in Ouput, that beginning 0 is gone, since numbers dont begin with 0. I want to retain that 0 in that number and also it should be one number and not a numerical array or string array, since the output is one number.
  2 Comments
Stephen23
Stephen23 on 9 Jan 2019
Edited: Stephen23 on 10 Jan 2019
"I want to retain that 0 in that number and also it should be one number and not a numerical array or string array, since the output is one number."
What you are asking for is not possible: most numeric types used by modern computers do not store leading zeros.
(for their closest displayed decimal representations anyway... of course what happens with the actual binary numbers stored in memory is another kettle of fish entirely...)
In any case, telephone numbers, post codes, credit card numbers, social security numbers, PINs, etc are not numbers in any useful mathematical sense: they are simply a string of characters that happens to be represented using digits. Mathematical operations on them have no meaning, and as such representing them as a single number is misleading.
James Tursa
James Tursa on 9 Jan 2019
I understand what you intend by this comment, but in point of fact half of the integers store leading 0 bits, and denormalized floating point numbers also store leading 0 bits. This is more of a display issue than a storage issue.

Sign in to comment.

Answers (3)

Guillaume
Guillaume on 9 Jan 2019
no integer or floating point types store leading zeros
Or rather, the OP is confusing the typographical representation of the number with the number itself. The number six is the number six, it does not a have a leading (or trailing zero), nor does the number one third, etc. Now we can represent that number six using any number of typographical conventions such as 6, VI, IIIIII, 110b, or 06 or even 00000000000006. These are all typographical representations of the number, not the number itself.
Matlab, like many other programming languages use the most succinct typographical representation of numbers, which is to not display any of the infinity of zeros that could possibly be written before the number. If you want something different then you'll have to store the typographical representation that you want as a char array or string, instead of the number itself. Possibly,
OutputString = compose(compose('%%0%dd', numel(Input)), Output)

Walter Roberson
Walter Roberson on 9 Jan 2019
That is not possible in MATLAB . The default numeric display routines always drop leading 0 except in the 1s position or sometimes in engineering notation .

Govind Sankar Madhavan Pillai Ramachandran Nair
Thank You. I got the answer.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!