Shop OBEX P1 Docs P2 Docs Learn Events
Help splitting a number into 2 parts — Parallax Forums

Help splitting a number into 2 parts

Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
edited 2008-09-04 03:14 in Propeller 1
I need to split a number into 2 numbers.
Example: 52 becomes x:=5 and y:=2

I'm using decx from Simplenumbers to grab the whole number
with this: s:= SN.decx(second,2)

Can someone help me with a routine to split both sides into two
different numbers into x & y?

Thanks
OBC

(Here's the decx routine)
PUB decx(value, digits) | div

'' Returns pointer to zero-padded, signed-decimal string
'' -- if value is negative, field width is digits+1

  clrstr(@nstr, MAX_LEN)  
  digits := 1 #> digits <# 10

  if (value < 0)                                        ' negative value?   
    -value                                              '   yes, make positive
    nstr[noparse][[/noparse]idx++] := "-"                                  '   and print sign indicator

  div := 1_000_000_000                                  ' initialize divisor
  if digits < 10                                        ' less than 10 digits?
    repeat (10 - digits)                                '   yes, adjust divisor
      div /= 10
  
  value //= (div * 10)                                  ' truncate unused digits
  
  repeat digits
    nstr[noparse][[/noparse]idx++] := (value / div + "0")                  ' convert digit to ASCII
    value //= div                                       ' update value
    div /= 10                                           ' update divisor

  return @nstr


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?

Getting started with a Propeller Protoboard?
Check out: Introduction to the Proboard & Propeller Cookbook 1.4
Updates to the Cookbook are now posted to: Propeller.warrantyvoid.us
Got an SD card connected? - PropDOS

Comments

  • mirrormirror Posts: 322
    edited 2008-09-04 00:57
    v := 52
    x := v / 10
    y := v // 10

    or

    v := string('52')
    x := v[noparse][[/noparse] 0 ] - "0"
    y := v[noparse][[/noparse] 1 ] - "0"

  • hippyhippy Posts: 1,981
    edited 2008-09-04 00:59
    52 becomes x:=5 and y:=2 ...

    n := 52
    x := n / 10
    y := n // 10

    or are you after something ore complicated ?
  • BTXBTX Posts: 674
    edited 2008-09-04 01:00
    Hey OBC.
    Am I wrong or:
    x := N/10
    y := N - (x*10)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Regards.

    Alberto.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2008-09-04 01:00
    An obvious solution! Why didn't I think of that!! Thanks guys.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    New to the Propeller?

    Getting started with a Propeller Protoboard?
    Check out: Introduction to the Proboard & Propeller Cookbook 1.4
    Updates to the Cookbook are now posted to: Propeller.warrantyvoid.us
    Got an SD card connected? - PropDOS
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2008-09-04 01:16
    The examples you guys gave work, but not with decx.
    I suspect because it is returning a string. An easy way to convert it?

    OBC

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    New to the Propeller?

    Getting started with a Propeller Protoboard?
    Check out: Introduction to the Proboard & Propeller Cookbook 1.4
    Updates to the Cookbook are now posted to: Propeller.warrantyvoid.us
    Got an SD card connected? - PropDOS
  • tpw_mantpw_man Posts: 276
    edited 2008-09-04 01:28
    Oh, well, then do this:
    straddr := <routine>
    n1 := byte[noparse][[/noparse]straddr]
    n2 := byte[noparse][[/noparse]straddr+1]
    n1 -= "0"
    n2 -= "0"
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I am 1011, so be surprised!


    Advertisement sponsored by dfletch:
    Come and join us on the Propeller IRC channel for fast and easy help!
    Channel: #propeller
    Server: irc.freenode.net or freenode.net
    If you don't want to bother installing an IRC client, use Mibbit. www.mibbit.com
    tongue.gif
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2008-09-04 01:39
    That was the ticket! Now explain what this does?

    OBC

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    New to the Propeller?

    Getting started with a Propeller Protoboard?
    Check out: Introduction to the Proboard & Propeller Cookbook 1.4
    Updates to the Cookbook are now posted to: Propeller.warrantyvoid.us
    Got an SD card connected? - PropDOS
  • TimmooreTimmoore Posts: 1,031
    edited 2008-09-04 01:43
    The 2nd and 3rd line isolate the characters, in your example "5" and "2". The 4th and 5th line subtract the binary value of "0" from them so "5" which is $35 (asci for "5") becomes 5 since "0" is $30.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2008-09-04 03:14
    Thanks guys... implemented and learned! Used in the ppdb_digital_clock code.

    OBC

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    New to the Propeller?

    Getting started with a Propeller Protoboard?
    Check out: Introduction to the Proboard & Propeller Cookbook 1.4
    Updates to the Cookbook are now posted to: Propeller.warrantyvoid.us
    Got an SD card connected? - PropDOS
Sign In or Register to comment.