seperating a number into individual values.
Dgswaner
Posts: 795
I'm trying to separate a 5 digit number into 5 separate values the number, 12345 needs to get separated out into 1 2 3.... I thought I had the solution with the TRUNC function. my thought was to divide the value by 10 (12345/10 = 1234.5) use the TRUNC function to return only 5, the remainder.
I can't find any examples or figure out the syntax, and from what I've tried and read TRUNC has to be used in the CON section of code, I would greatly. Will TRUNC work for this application? is there an easier way to do this?
I would greatly appreciate some direction.
added:
oops, ya I can count! fixed my "5 digit number"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster
DGSwaner
Post Edited (Dgswaner) : 5/14/2008 8:38:06 PM GMT
I can't find any examples or figure out the syntax, and from what I've tried and read TRUNC has to be used in the CON section of code, I would greatly. Will TRUNC work for this application? is there an easier way to do this?
I would greatly appreciate some direction.
added:
oops, ya I can count! fixed my "5 digit number"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster
DGSwaner
Post Edited (Dgswaner) : 5/14/2008 8:38:06 PM GMT
Comments
as SPIN does only INTEGER-Math the "normal" division by 10 will do this.
Value := 12345
loop-start
SubstractVal := Value / 10
singleDigit := Value - SubstractVal
Value := SubstractVal
end of loop
Another Idea: take a look at the PUB "dec" inside FullDuplexSerial
best regards
Stefan
Value := 12345
loop-start
SubstractVal := Value / 10
(SubstractVal holds the number "1234" at this point. So......)
SubstractVal := SubstractVal * 10
(This will make the subtraction look like 12345 - 12340 giving the expected 5, where as 12345-1234 yeilds 11111)
singleDigit := Value - SubstractVal
Value := SubstractVal
end of loop
you are right !
i should not post "quick and dirty" without having thought it through to the end.
And there is another calculation nescessary
Value := 34758
loop-start
SubstractVal := Value / 10
(SubstractVal holds the number "3475" at this point. So......)
SubstractVal := SubstractVal * 10
(SubstractVal holds the number "34750")
(This will make the subtraction look like 34758 - 34750 giving the expected 8
singleDigit := Value - SubstractVal
(now divide SubstractVal by 10 again to get the rest of the digits)
(34750 / 10 = 3475)
Value := SubstractVal / 10
end of loop
Stefan
I believe you already used the Format object and you can use it to do this.
buf var byte[noparse][[/noparse]6] 'byte buffer to hold 5 digits + closing null
fmt.sprintf(@buf,string("%d"),someValue) 'convert value into decimal string
i:=0
repeat while byte[noparse][[/noparse]@buf][noparse][[/noparse]i] <> 0
· digit := byte[noparse][[/noparse]@buf][noparse][[/noparse]i++]
If you always want 5 digits you can use
fmt.sprintf(@buf,string("%05.5d"),someValue)
regards peter
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster
DGSwaner