Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to figure out decimals?

Status
Not open for further replies.

shahatul

Programmer
May 28, 2002
23
US
I have some numbers. How to figure out each number has how many decimal points?. Suppose user has entered following numbers:
100.01
100.001
100.0001

Thanks in advance.
Shah AV
 
What are you going to do with them? Why do you need to know how many the user entered?

You can set the format for data entry, keep, use and dispalay as many decimal places as you want (well, almost, as there is a limit).
 
If these are in a character field, then:
Code:
Function NumDecimals
LPARAMETER p_cString
LOCAL lnRetDecimals
lnRetDecimals = 0
p_cString = ALLTRIM(p_cString)
IF LEN(p_cString) = ;
   LEN(CHRTRAN(p_cString, CHRTRAN(p_cString, "0123456789.", ""), ""))
   IF OCCURS(".", p_cString) = 1 && good decimal number
      lnRetDecimals = LEN(p_cString)-AT(".",p_cString)
   ELSE && bad string
      lnRetDecimals = -1
   ENDIF
ELSE && bad string
   lnRetDecimals = -1
ENDIF
RETURN lnRetDecimals
*-------------------
?NumDecimals("123.5")
?NumDecimals("123.52999")
?NumDecimals(".52999")
?NumDecimals(" 0.52999")
?NumDecimals("123.52.999")
?NumDecimals("123x52.999")
?NumDecimals("123.52 999")
Rick
 
HI

Some correction in above... since 0 decimals will throw error in my above statement..
*****************************
n = Number
nDec = 0
IF n # INT(n/1)
nDec = LEN(SUBSTR(TRANSFORM(n),AT(".",transform(n))+1))
ENDIF
WAIT WINDOW STR(nDec)
******************************
:)


____________________________________________
ramani - (Subramanian.G) :)
 
Thanks Ramani and Rick for your great help.

Dear Ramani,
If you run "IF n # INT(n/1)" return false. But next statement "LEN(SUBSTR(TRANSFORM(n),AT(".",transform(n))+1))" definately works for me.

Thanks with warm regards

Shah AV

 
I use this snippet to import data and programtically decide what decimals and total width of a character field containing only numbers/decimals needs to have:

IF VarFieldType="N" &&if still a number

CALCULATE MAX(IIF("." $ &varfield=.f.,0,LEN((CHRTRAN(STR(VAL(&varfield),20,9),"0","")))-(1+LEN((CHRTRAN(STR(INT(VAL(&varfield))),"0","")))))) TO decimalcnt

decimalcnt=decimalcnt-IIF(decimalcnt=1,1,0)

CALCULATE MAX(LEN(ALLTRIM(STR(ROUND(val(&varfield),0),20)))) to basewidth for RECNO()>1
totwidth=basewidth+decimalcnt+IIF(decimalcnt>0,1,0)

ENDIF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top