Count Number Of Place

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
DarkRookie
User
User
Posts: 20
Joined: Wed Nov 24, 2010 5:47 pm

Count Number Of Place

Post 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.
It is better to have a dumb question than a wrong answer!
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Count Number Of Place

Post 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)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Count Number Of Place

Post 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
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Post Reply