PDA

View Full Version : seperating a number into individual values.

Dgswaner
05-15-2008, 02:21 AM
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.

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

StefanL38
05-15-2008, 02:35 AM
hello DGSwaner,

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

bambino
05-15-2008, 06:38 AM
Don't forget your place holder for the SubstractVal.
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

StefanL38
05-15-2008, 12:23 PM
hello bambino

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

Paul M
05-15-2008, 07:02 PM
Why not use the modulus function?

value:=28509
repeat 5
singleDigit:= value // 10
value/=10
.
.

Peter Verkaik
05-15-2008, 08:39 PM
Dgswaner,

I believe you already used the Format object and you can use it to do this.

buf var byte[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[@buf][i] <> 0
· digit := byte[@buf][i++]

If you always want 5 digits you can use
fmt.sprintf(@buf,string("%05.5d"),someValue)

regards peter

Dgswaner
05-15-2008, 11:21 PM
Wow Thanks for all of the help, I'm swamped with work at home, and haven't had a chance to try this. what I'm doing is reading 5 Ping sensors, then converting the reading to a value 1-8 combining them into a 5 digit number send it to my PINK "Base Station" Via Xbee separate out the values and write out each value to a PINK variable. then a web page will display an image indicating the distance an object is detected. I have most of it already working, this was the missing piece. Thanks again for the help everyone.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster

DGSwaner