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

Displaying Integers/real values as strings(VFP8) 1

Status
Not open for further replies.

foxbldr

Programmer
Apr 16, 2002
109
CA
Hi all,

Can somebody help me to do the following:

IF <field/variable> is an Interger>
STR(Field/Variable,15) && no decimals
endif

IF <field/variable> is a Real/Double>
STR(Field/Variable,15,2) && with 2 decimals
endif

I tried VARTYPE but, it returns 'N' for all numerics except Currency ('Y'). I am ADDITEMing combo/list boxes using specific values in tables.

Thanks

Foxbldr






 
Look at TRANSFORM()

? TRANSFORM(lcStr,&quot;999,999,999,999.99&quot;) && your choice of formatting

or

? TRANSFORM(lcStr)

Jim Osieczonek
Delta Business Group, LLC
 
Great, by the way. You can use TRANSFORM with a lot of other datatypes like DATE, DATETIME, etc. It is a handy function to use.

Jim Osieczonek
Delta Business Group, LLC
 
Since you said earlier ' && no decimals', you may want to try something like:
Code:
IF MOD(<field/variable>, INT(<field/variable>)) = 0  &&... integer
   cString = TRANSFORM(<field/variable>, '999,999,999,999,999')    && no decimals
ELSE 
   cString = TRANSFORM(<field/variable>, '999,999,999,999,999.99')    && decimals
ENDIF


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Transform() works fine here, automagically dropping the decimal points if a real number rounds to an integer.

But there may be instances where you want to display a real number of value 8 as &quot;8.00&quot;, rather than &quot;8&quot;.

Due to xBASE's loose typing, I don't believe it's possible to determine if a variable is an integer or a real, but you can use AFIELDS() to determine the data type of a column in a table:

? GetDataType(&quot;atCustomers&quot;, &quot;CUTSTAMP&quot;)

function GetDataType
lparameters strTable, strCol
intCurrArea = select()
select 0
use (strTable) again
=afields(arrTableStru)
intNameElement = ascan(arrTableStru, upper(strCol))
strDataType = arrTableStru(intNameElement+1)
use
select (intCurrArea)
return strDataType
endfunc
 
Seadriver,

Wonderful! I accepted Jim's suggestion as I am ADDITEMing combo/list boxes using table-columns and a column has a definite data type(Hey,we all are aware of that!). Though I am not bumping into a situation like you suggested in my case, I consider yours as a good tip.

Keep up the good work!

Foxbldr










 
I don't believe it's possible to determine if a variable is an integer or a real

When you say not sure if you can determine if it is a whole number, or not, I am not sure I am following you.

Example.

lnMyNumber = 10.00
? MOD(lnMyNumber,1)
* returns 0 because it is whole number

lnMyNumber = 10.23
? MOD(lnMyNumber,1)
* returns the remainder .23

Another way I have seen it done
? = lnMyNumber - INT(lnMyNumber)


Use this along with SET DECIMALS and then build your TRANSFORM string accordingly.


Jim Osieczonek
Delta Business Group, LLC
 
Wow, my first star! [smile] Thanks!

Jim, my point is that 10.00 is a real number that happens to round to an integer value. They're not the same thing, and sometimes you want to treat/display them differently.
 
Okay, I am following you better.

What I find strange is that you can define the number as an interval (vfp8), but then do whatever you want to the number. I would have expected it to truncate the decimail.

Example 1:
LOCAL lnnumber as Integer
lnnumber = 10 - .5
? lnnumber
* diplays 9.50 based on decimal setting 2.

Example 2:
LOCAL lnnumber as Integer
lnnumber = 9.50
? lnnumber
* also diplays 9.50 based on decimal setting 2.

What's the value of defining it as an integer?

Jim Osieczonek
Delta Business Group, LLC
 
Jim,

I don't have VFP 8, so I can't verify that the &quot;As Integer&quot; is supported, but this syntax works just fine in VFP 6 as well.

I believe that the parser ignores everything after the variable name. You can prove this by compiling this:

local lInt As whatchamacallit

Compiles fine! So does:

local lInt whatchamacallit

If you &quot;? lInt&quot;, you get the logical .F., the value of an unitialized variable, just like an empty parameter.
 
Looks like Intellisense is the only thing that really cares about 'AS'. Look up 'LOCAL command' in the MSDN:

&quot;The strong typing required by the CodeSense parser in IntelliSense is available only when you create strongly typed object and variable references by using the optional AS clause. &quot;


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top