Shop OBEX P1 Docs P2 Docs Learn Events
Counting the number of numbers, in a number — Parallax Forums

Counting the number of numbers, in a number

grasshoppergrasshopper Posts: 438
edited 2008-12-12 01:17 in Propeller 1
I am trying to count how many numbers I have in a number. For example the number 1562 has 4 numbers the 1,5,6,2. I can do this using math and logic, but I wanted to know if there was any other way to achieve this preferably with out using any math or divide statements.

Thanks

Comments

  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-12-10 03:13
    If you don't want to do divide, you could always have a repeat loop and check if the number is less than...

    
    
    X := 9 repeat i from 1 to 9
      if(number <= x)
        NumberOfDigits := i
        break  (or whatever spin equivalent is... I forgot)
      x := x * 10 + 9
    

    I don't think there is a simple one-shot process for doing this, but I'm probably wrong... people on this forum amaze me with their tricks consistantly.
  • Lord SteveLord Steve Posts: 206
    edited 2008-12-10 05:29
    The one-shot process to do this is to take the logarithm (in this case base-10) of the number and the integer part of the logarithm PLUS ONE gives you the number of digits in that base.· That is more complicated than divide, though.
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-12-10 07:52
    hello grasshopper,

    I think it can't be done within one line

    if you don't want a loop

    you could use a case-construction

    case MyVariable
    1..9
    10..99
    100...999

    etc.

    but I think the best way is the way like the method "DEC" in the FullDuplexSerial-object does it

    Your asking for ONE detail. What is it good for ? If you describe your COMPLETE target there might be still other ways to achive it

    best regards

    Stefan
  • grasshoppergrasshopper Posts: 438
    edited 2008-12-10 14:42
    StefanL38

    That might be what I am looking for. Currently I use a bit of math and have to float not float etc. Ill test this today. Thanks
  • RiJoRiRiJoRi Posts: 157
    edited 2008-12-10 22:48
    How about something like this:

    Base := 10 ' or 2, 4, 8, 16, etc.
    Digits := 0
    Limit := 1
    repeat while Limit =< Number
        x *= Base
        Digits++  ' count the number of Base digits in Number
    
    



    I am at work, and my P'lax stuff is home, so I'm not too sure of the syntax. Also, I've done a sheet-of-paper check, and I BELIEVE the algorithm is good, but "buyer beware"!

    --Rich
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2008-12-11 20:12
    The subject line threw me for a loop. =) Maybe number of digits in a number?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
  • DosManDanDosManDan Posts: 179
    edited 2008-12-11 22:44
    Make it nice and easy, no math at all - This will do 4 digits (9999). I don't use Spin, so I don't know the exact coding of an IF statement for the Prop, but the logic below should be easy to code.

    If X >= 1000 Then
    · Count = 4
    ElseIf X >= 100 Then
    · Count = 3
    ElseIf X >= 10 Then
    · Count = 2
    Else
    · Count = 1 'Zero counts as one digit
    End If
  • grasshoppergrasshopper Posts: 438
    edited 2008-12-12 01:17
    DosManDan, Yea this is sort of the way I went, but fitted it so that it could handle a negative and floating point number. Thanks all for the help
Sign In or Register to comment.