roundingvisual-foxpro

rounding issue - VFP


From Native VFP9 command window:

lyTotal_Revenue=16571.39
lyExcludeFromRent=1673.88
lnRentPercent=30
lnanswer=((lyTotal_Revenue - lyExcludeFromRent) * (lnRentPercent / 100))
?lnanswer shows 4469.253

That is the correct answer.

I have a report that has a field. The field's format expression is 9,999,999.99 The field's format options is Currency The expression on the field is round(((lyTotal_Revenue-lyExcludeFromRent) * (lnRentPercent/100)),2) Why does the report show $4,969.26


Solution

  • I finally realized what the issue is. My report has the normal stuff meaning detail lines and a total line. Columns are Revenue and ExcludeFromRent. The total line is simply the sum of the detail lines. So I have the same fields on the total line but their Calculation tab has Sum on Report. I also have a summary page. That summary page does some math on the values from the total line. The math in pseudocode is Rent Revenue = (Total Revenue minus Total ExcludeFromRent). Then AmountToPay = Rent Revenue * (lnRentPercent / 100). So, the field in the summary section has an expression of round(((Revenue-ExcludeFromRent) * (lnrentpct/100)),2) and the Calculation tab has Sum on Report. What is wrong with this? Everytime a detail line gets processed the expression on the summary field is executed. That means I am rounding each detail row and adding that value to the summary field. What I did instead: I calculate the sum of the Revenue and ExcludeFromRent before calling the report and then execute the expression round(((Revenue-ExcludeFromRent) * (lnrentpct/100)),2) ONCE and place the value in a private variable. The expression for the summary field is the private variable and the Calculation tab says None.

    John

    EDIT: Any variable, array, for property etc are visible from within the report (because report is a command running technically within the same routine). ie: (discard the correctness of calculation I am doing, it is not, I know)

    local lnTotalRevenue, lnExcludeFromRent, lnPercent
    local array laRevenue[1]
    select sum(Revenue) from myTable into array laRevenue
    
    lnPercent = 30
    lnTotalRevenue = round(laRevenue[1] - m.lnExcludeFromRent,2)
    thisform.Tag = 'Rent Information'
    
    report form MyReport ...
    

    Within the report you have access to all those lnTotalRevenue, lnExcludeFromRent, lnPercent, laRevenue, thisform.Tag. This may be of great value sometimes.