Shop OBEX P1 Docs P2 Docs Learn Events
seperating a number into individual values. — Parallax Forums

seperating a number into individual values.

DgswanerDgswaner Posts: 795
edited 2008-05-15 16:21 in Propeller 1
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

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2008-05-14 19:35
    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
  • bambinobambino Posts: 789
    edited 2008-05-14 23:38
    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
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-05-15 05:23
    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 MPaul M Posts: 95
    edited 2008-05-15 12:02
    Why not use the modulus function?
        value:=28509
        repeat 5
          singleDigit:= value // 10
          value/=10
        .
        .
    
     
    
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-05-15 13:39
    Dgswaner,

    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
  • DgswanerDgswaner Posts: 795
    edited 2008-05-15 16:21
    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
Sign In or Register to comment.