Page 1 of 1

Count Number Of Place

Posted: Mon Dec 16, 2013 8:07 pm
by DarkRookie
Can we get a function that will count the number of digits/places in a number.
I tried searching for a function in the manual for one, but couldn't find anything.

I made:

Code: Select all

Macro IntLen(Number)
  Len(Str(Number))
EndMacro
To do it.

It works perfectly. Just looks blerg and not right to me.

Re: Count Number Of Place

Posted: Mon Dec 16, 2013 8:32 pm
by skywalk
If you can ignore floating point...

Code: Select all

Procedure.i ML_nDigits(IntNumber.q)
  Protected.i nDigits
  If IntNumber <> 0
    While IntNumber <> 0
      IntNumber / 10
      nDigits + 1
    Wend
  Else
    nDigits = 1
  EndIf
  ProcedureReturn nDigits
EndProcedure

Debug ML_nDigits(9223372036854775807)
Debug ML_nDigits(0)
Debug ML_nDigits(1.23456)

Re: Count Number Of Place

Posted: Tue Dec 17, 2013 6:11 am
by Shield
Another solution:

Code: Select all

Procedure.i NumberLength(number.q)

	If number = 0
		ProcedureReturn 1
	EndIf
	
	If number < 0
		ProcedureReturn Int(Log10(-number)) + 2 ; +2 for storing the sign.
	Else
		 ProcedureReturn Int(Log10(number)) + 1
	EndIf
EndProcedure