Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing the number of decimal places

777 views
Skip to first unread message

Andy Trezise

unread,
Sep 29, 2009, 6:02:45 PM9/29/09
to
I have been developing a financial application that stores numerical data in
fields specified as NUMERIC 12,6

I am unsure about how best to 'round' the values (when I only want to
display to something other than 6 decimal places).

If I use SET DECIMALS TO 2 & SET FIXED ON (as I do in the LOAD event of all
my forms) then I would expect normal behaviour to round 1.657001 to 1.66 but
when I display the data in a grid (for example) it shows 1.65 and is
therefore misleading to the user.....

If I display the value using the ROUND(x,2) function then the value appears
correctly.

What am I doing wrong? I don't want to have to ROUND every value before
displaying it.

There are occasions where I would want to show certain fields to 6 decimal
places and others to 2 (for example) ..... as the user may not require that
amount of precision. Using SET DECIMALS will effect ALL data and as such may
not be the best way of going about things. I always thought that using a
formating mask such as 999999.99 was the same as ROUND(x,2) but obvously
not?


Christof Wollenhaupt

unread,
Sep 30, 2009, 1:49:11 AM9/30/09
to
Hi Andy,

>I would expect normal behaviour to round 1.657001 to 1.66

Nope, this is not what SET DECIMALS does and is not what the help file says.
VFP keeps track of the number of decimal places for numbers. SET DECIMALS is
used when VFP cannot determine how many decimals the result should have. For
instance:

SET DECIMALS TO 2
? 1/3
? 1/3.000

Results in
0.33
0.333

The first expression has a low number of decimals, therefore VFP uses the
SET DECIMALS value. The second expression has a higher number, therefore
that precision is used.

> If I display the value using the ROUND(x,2) function then the value
> appears correctly.

And that's your only choice, either in the ControlSource, when assigning the
value property or in the SELECT query. VFP does not modify data before
displaying. Rather anything you pass is merely formatted and then shown.


--
Christof

Andy Trezise

unread,
Sep 30, 2009, 3:21:04 AM9/30/09
to
Thanks Christof

But what's confusing me is that if I try from the VFP command prompt.....

x=3.345

SET DECIMALS TO 2

SET FIXED ON

?x

VFP displays rounds the number UP to 3.35 as I would expect - without me
having to use ROUND().

more bizzare, if I then do

?ROUND(x,1)

VFP displays 3.30 when I would have expected it to show 3.40.....i.e.
ROUND() hasn't rounded at all it's just truncated the numbers.

It just doesn't make sense to me.


"Christof Wollenhaupt" <msnews.microso...@foxpert.com> wrote in
message news:euiOsGZQ...@TK2MSFTNGP06.phx.gbl...

Christof Wollenhaupt

unread,
Sep 30, 2009, 5:09:14 AM9/30/09
to
Hi Andy,

> But what's confusing

VFP's behavior is confusing. Moreover, it's highly inconsistent.

> SET FIXED ON


> VFP displays rounds the number UP to 3.35 as I would expect

That's the result of the SET FIXED command. When the number has more decimal
places than specified by SET DECIMALS, then SET FIXED will cause numbers to
be rounded when the result is displayed. The latter is the key part as SET
FIXED only applies to certain commands and functions. Basically, these are
the print commands ? and ?? , the TRANSFORM() function, the textmerge
capability, and the old pre-FRX report commands.

SET FIXED does not apply to @...GET or the Value property in objects. It
also doesn't apply to any function that converts using the non-result
displaying algorithm. This includes the PADx() functions and the CAST()
function.

> ?ROUND(x,1)
> VFP displays 3.30 when I would have expected it to show 3.40.....

SET DECIMALS, SET FIXED, ?, etc. only apply to the display of a number. The
number itself doesn't change at all. It's still 3.345 and remains 3.345.
When you ROUND(3.345,1), the correct result is 3.3. Due to SET FIXED being
in effect this is displayed as 3.30. Even so x is shown as 3.35, that's not
that value used for subsequent operations.


--
Christof

Andy Trezise

unread,
Sep 30, 2009, 6:38:06 AM9/30/09
to
I think what you're saying Christof is that I need to continue using ROUND()
in all my arithmatic / expressions to ensure values are evaluated correctly.

Even so....if I have a number held to 6 decimal places but only want the
user to input 2 (some customers don't care about the precision but I want
the database to remain consistent) how do I display the initial value in a
textbox (using my base tables field as the controlsource) and ensure
rounding takes place correctly? if the number is 3.145600 and I use an input
mask of 999999.99 then the initial value shown in the field is 3.14. If I'm
using ROUND() everywhere else (including the display of data in grids etc)
then the two values are more than likely to appear to differ (even though we
know they are the same).

"Christof Wollenhaupt" <msnews.microso...@foxpert.com> wrote in

message news:uXAgi2aQ...@TK2MSFTNGP04.phx.gbl...

Christof Wollenhaupt

unread,
Oct 1, 2009, 6:35:42 PM10/1/09
to
Hi Andy,

>I think what you're saying Christof is that I need to continue using
>ROUND() in all my arithmatic / expressions to ensure values are evaluated
>correctly.

Exactly.

> how do I display the initial value in a textbox (using my base tables
> field as the controlsource) and ensure rounding takes place correctly?

You can't use the field as a control source. There are several options:

- You can use a property as a controlsource. Add an access and assign method
to read and write the value into the table performing a conversion.

- Don't use a ControlSource. Instead assign the Value property in the
Refresh and GotFocus event. In Valid check if the value has changed, and if
so, write the new value back to the table.


--
Christof

Andy Trezise

unread,
Oct 2, 2009, 4:38:11 AM10/2/09
to
Thanks again Christof

Access & Assign methods seem the way to go.....I'd never even thought of
that before you mentioned it.

Cheers


"Christof Wollenhaupt" <msnews.microso...@foxpert.com> wrote in

message news:%23MK4Reu...@TK2MSFTNGP05.phx.gbl...

0 new messages