Extracting single digits from a 4 digit decimal number
Gerry Shand
Posts: 45
Hi All:
I am looking for a piece of code that will allow me to extract a single digit from a 4 digit decimal number.
As an example, I have a a program that does some computations and comes up with the result 1068. The range is from 0000 to 9999. All results are in decimal so I am only dealing with 0-9 in each position - no special symbols.
I was wondering how do I get at the individual digits (1, 0, 6 and 8 in the above example) to manipulate them individually? Once the digits have been isolated, I can then sequence them into a serial LCD with the option of inserting characters between the numbers. So I can end up displaying a result like: 1.0$6&8
I have tried using an array but it's not working.
Any thoughts on this? Thanks in advance for your help.
Regards,
Gerry
I am looking for a piece of code that will allow me to extract a single digit from a 4 digit decimal number.
As an example, I have a a program that does some computations and comes up with the result 1068. The range is from 0000 to 9999. All results are in decimal so I am only dealing with 0-9 in each position - no special symbols.
I was wondering how do I get at the individual digits (1, 0, 6 and 8 in the above example) to manipulate them individually? Once the digits have been isolated, I can then sequence them into a serial LCD with the option of inserting characters between the numbers. So I can end up displaying a result like: 1.0$6&8
I have tried using an array but it's not working.
Any thoughts on this? Thanks in advance for your help.
Regards,
Gerry
Comments
b1=1068 ' your number
b2=b1/10*10 ' now 1060
0nes=b1-b2 ' now 8
b1=b1/10 ' now 106
b2=b1/10*10 ' now 100
Tens=b1-b2 ' now 6
etc...
'
'DEFINE VARIABLES
'
DisplayData VAR Word
DD1 VAR Word
DD2 VAR Word
Ones VAR Nib
Tens VAR Nib
Hundreds VAR Nib
Thousands VAR Nib
Program snippet
DD1=DisplayData
DD2=DD1/10*10
Ones=DD1-DD2
DD1=DD1/10
DD2=DD1/10*10
Tens=DD1-DD2
DD1=DD1/10
DD2=DD1/10*10
Hundreds=DD1-DD2
DD1=DD1/10
DD2=DD1/10*10
Thousands=DD1-DD2
This works very well and only uses two variables for the number crunching. The end results have to be stored in nibble sized variables (4 in total here).
Many thanks. My wife is impressed I actually got this up and running in a weekend.
Regards,
Gerrry S
Further, you could use a subroutine for the two equations to save a bit of space.
If you don't need the intermediate results, you can put the DIG operator in your SEROUT command: