How to add one digit to another digits?
Afficher commentaires plus anciens
For example, I have a number b=5; and i need to add it by another digits, for example 1234 and get the answer 12345. Code may show like: set(handles.text1,'string',1234+b) An error is "1234+b" and i don't know how format it that one digit adds to another digits. Thanks in advance.
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 1 Mar 2011
The answer to your question as phrased is:
set(handles.text1, 'string', 1234*10^max(1,ceil(log10(b+1))) + b)
I would suggest, though, that you instead consider
set(handles.text1, 'string', sprintf('%d%d', 1234, b))
EDIT: put in max() to correct for the possibility that b is 0.
4 commentaires
Eggo Ogge
le 1 Mar 2011
Paulo Silva
le 1 Mar 2011
Walter's solutions are always very good
Eggo Ogge
le 2 Mar 2011
Walter Roberson
le 2 Mar 2011
sprintf('%s%s', '=a21:', '2F4');
Or more simply
['=a21:', '2F4']
It is not possible to create a _numeric_ value in hex format.
Perhaps, though, you might like to use
sprintf('%s%x', '=a21:', hex2dec('2F4'))
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!