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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine number of decimal places in Numeric field? 2

Status
Not open for further replies.

mroth36

Programmer
Jul 21, 2005
27
0
0
Hi!
I am using the TYPE function, to determine a field type. Is there a way to determine the number of decimal places of a numeric field, in a case where the decimal amounts are 0 (ex: 10.00)?

Thank You
 
afields() creates an array with the table structure you can utilize.

Brian
 
MROTH,
Your question seems to cross horses mid stream... are you trying to determine the number of decimal places in a Field, or the number of decimal places that a variable has as it's value? There are certainly different approaches, and baltman's is probably best if you are interested only in the field "storage" values. If you want to know how many decimals are in a value stored in a variable, this is a little trickier, as things like SET DECIMALS TO will affect the "display" of that value.

If you don't need the exact number of decimal places that one value is "Larger" than the the other, and you only want to know if it contains a decimal value, the safest way is to do this:

Code:
nValue = 1.23456
IF nValue - INT(nValue) > 0
    ? 'nValue has Decimal Places'
ELSE
    ? 'nValue is an Integer'
ENDIF

Because of the influence of SET DECIMAL's, even numeric values that have "No decimals" will appear to have them so, 10 with SET DECIMAL to 2 is going to have a value of 10.00 to the above is not going to be 100% fool proof when testing values, but that should have little impact.



Best Regards,
Scott

"Everything should be made as simple as possible, and no simpler."[hammer]
 
If your horse is on that side of the stream, try this:

Code:
lnNum=10.00
lnDec=IIF("." $ TRANSFORM(lnNum)=.f.,0,;
  LEN(RTRIM(CHRTRAN(RIGHT(TRANSFORM(ROUND(lnNum,19)),;
  LEN(TRANSFORM(ROUND(lnNum,19)))-;
  ATC(".",TRANSFORM(ROUND(lnNum,19)),1)),[0],[ ]))))
?lnDec

Brian
 
Hi everyone & thanx for your response.'s My internet was down so I chouldn't get in. Let me rephrase my question: I am 'dumping' data from a dbf & do not know beforhand if a numeric field has any decimal places. I CAN determine that its numeric, using the Type function, but I need to determine if it has any decimal places (if the decimal is .00 or the decimal has a value of .01 thru .99). I tried both of the above routines and if the decimal is .00, it tells me that there are ZEROE decimal places. The only function that tells me that the field has 2 decimal places is the afields()function mentioned by Baltman. This is more cumbersome but do I have a choice?

Thank You all!
 

Mroth,

I think what you are saying is that you want to know how many decimal places are allocated in the DBF -- how much space is available for the decimal places, if you will. You are not asking to know how many places exist in any given instance of the field.

If that's right, Brian's AFIELDS() method is the way to go.

You say it's cumbersome, but it would be easy to write a little function that receives the table name and the field name as parameters, than does an AFIELDS(), followed by an ASCAN(), and returns the number in question.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
A more concrete way is to USE the table and 'COPY STRUCTURE TO MyTableStructure EXTENDED'. The resulting table is the same data available in afields().

Why do you need to know the decimals in the DBF? Perhaps you're making things harder than you need to.

BTW- Please note the 'Thank User for this valuable post!' as I see you haven't noticed it yet.

Brian
 
mroth,
Not fully understanding your requirment, why don't you just assume maximum decimal value for any one field then? If you "Know and detect" it is numeric, and the max decimals is 2, why not make all numeric values with 2 decimal places. The impact of that from a strictly numeric perspective is you 9 become 9.00 and you 10 become 10.00, but you 2.37 remains 2.37 Adding decimals to integer values really has no impact on them, in 99.9% of end results. Consider that, and then as Mike suggests, maybe you are just making this too hard. Fox will automatically add the decimals, so there is nothing you need to do.



Best Regards,
Scott

"Everything should be made as simple as possible, and no simpler."[hammer]
 
Hi,
I wrote a function which dump's data from a requested dbf, and whould like to know if a numeric field
has decimals. I use this dump while I am coding & need to code my report output accordingly.

Thank You
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top