Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

SET DECIMALS, SET FIXED, Set(“Decimals”), Set(“Fixed”)

These two commands determine display and calculation of numbers involving decimals. SET DECIMALS determines the minimum number of decimals used and displayed. SET FIXED determines whether the SET DECIMALS value is also the maximum number displayed.

Usage

SET DECIMALS TO [ nDecimals ]
SET FIXED ON | OFF
nDecimalSetting = SET( "DECIMALS" )
cIsItFixed = SET( "FIXED" )

When you perform a calculation involving decimal numbers, SET DECIMALS sort of determines the number of decimal places in the result. To be more specific, it determines the number of decimal places you’ll see in the result, if the result has more places than the current setting. The correct calculation is performed and stored internally, but all subsequent displays of the result use the DECIMALS setting at the time the number was calculated. (If the result has fewer places than the DECIMALS setting and FIXED is OFF, the result is not padded with zeroes.)

If that sounds confusing, it’s because it is. Try this:

X1 = 10/3       && with default DECIMALS setting of 2
? X1            && 3.33, as expected
SET DECIMALS TO 5
X2 = 10/3
? X2            && 3.33333 - so far, so good
DISPLAY MEMORY LIKE X*

Interesting—X1 shows up as 3.33 while X2 is 3.33333. But it gets stranger.

? X1*2          && 6.67
? X2*2          && 6.66667
SET DECIMALS TO 18
? X1*2          && 6.67
? X2*2          && 6.66667
? X1*3          && 10.00
? X2*3          && 10.00000 - so no precision was lost in either case

The variables remember how many decimal places they were created with, even though you can see in the memory listing that the internal representations are the same.

What does all this mean for you? That you should choose a decimals setting for your application and use it throughout.

SET DECIMALS TO without a number resets you to the default of 2, even if you've set a different default through the Tools | Options dialog.

SET FIXED is much easier. When you turn it on, every number is displayed with the DECIMALS setting, even if the actual result is shorter or longer. That is, it rounds or pads numbers to exactly the specified number of decimal places.

SET DECIMALS affects Currency and Integer values only when SET FIXED is ON. In that case, Integer values are shown with the number of decimal places indicated by SET(“DECIMALS”). Currency is more interesting—the number of decimal places shown can be decreased if SET(“DECIMALS”) is less than four, but it never goes above four.

Both FIXED and DECIMALS are scoped to data sessions.

Example

* Using X1 and X2 created above
SET DECIMALS TO 3
SET FIXED ON
? X1       && 3.333
? X2       && 3.333
? X1*2     && 6.667
yMoney = $37.5837
? yMoney   && 37.584
SET DECIMALS TO 7
? yMoney   && 37.5837

See Also

Set, Set SysFormats