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

Round Price

Status
Not open for further replies.

Movi28

Systems Engineer
Feb 12, 2021
5
0
0
CH
Hi everybody,

I made a script for a client who wanted to put all items under $1 at $1
This works:

tableOpen('prix')
UPDATE prix ;
SET PR_PRIX = 1;
where ;
PR_PRIX < 1
Now he would like to do 2 other things and I am completely stuck.

He would like that if an item is between 1 and 10$ he wants to round it up like this: 1.12$ to 1.20
and from 10$ he wants to round it like this: 10.40 to 11$.

Can you help me ? [pc2]

thanks [bow]
Vince
 
Actually, this is quite easy. The secret is to use the ROUND() function. You pass it the number that you want to round, and the number of decimal places. If the latter is negative, it will round to a multiple of ten.

Here are some examples of how it works, which I have pasted from the VFP Help file:

Code:
? ROUND(1234.1962, 3) && Displays 1234.1960
? ROUND(1234.1962, 2) && Displays 1234.2000
? ROUND(1234.1962, 0) && Displays 1234.0000
? ROUND(1234.1962, -1)  && Displays 1230.0000
? ROUND(1234.1962, -2)  && Displays 1200.0000
? ROUND(1234.1962, -3)  && Displays 1000.0000

To round a number up to a multiple of 0.1 - as in your example of rounding 1.12 to 1.20 - you first add 0.05 to it:

Code:
? ROUND(1.12 + .05, 1)  && Gves 1.2

So more generally, you could do this:

Code:
UPDATE prix ;
  SET PR_PRIX = IIF(PR_PRIX < 10, ROUND(PR_PRiX + 0.05, 1), ROUND(PR_PRIX, 0))

Note that the number of decimal places you actually see on the screen or in reports depends on SET DECIMALS, but this does not affect how the number is actually stored.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top