$doctitle = "Accurate Rounding"; $authname = 'Lance Leonard'; $versions = "Applies to: All Versions"; $postdate = "13 October 2000"; // 0=Hides areainfo block, 1=About, 2=Paradox, 3=Delphi, 4=Assorted, 5=WebStuff $pagearea = 2; $audience = "1"; $navlinks = 'Paradox'; $metakeys = "paradox objectpal number currency round rounding"; $metadesc = "Describes how to accurately round numbers to set decimal places."; ?> include( $DOCUMENT_ROOT . "/lib/pageinit.php" ); ?>
Answer: Here's a custom method that does this for numbers:
method roundNumber( numValue Number, siDecimals smallInt ) number
var
strRounded String ; holds the rounded value
strFmtSpec String ; holds the format specification
endVar
strFmtSpec = "w." + string( siDecimals ) + ",EDW"
strRounded = format( strFmtSpec, numValue )
return number( strRounded )
endMethodWith thanks to Bertil Isberg for helping make this more "International friendly."
To use this with Currency values, replace references to "Number" with "Currency".
It's often a good idea to periodically round your field values to a specified precision. This prevents subtle variations from appearing due to calculations performed against your data values.
The following code is based on the Orders table provided as a Paradox sample table and rounds three fields to two decimal places:
method run(var eventInfo Event)
var
tcOrders tCursor ; pointer to the Orders table.
endVar
const
ERRTITLE = "Can't Round Numbers" ; error title.
endConst
if not tcOrders.open( "ORDERS" ) then
errorShow( ERRTITLE, "Orders did not open..." )
else
tcOrders.edit()
scan tcOrders :
Message( "Rounding record ", tcOrders.recNo(),
" of ", tcOrders.nRecords() )
tcOrders."Total Invoice" =
roundNumber( tcOrders."Total Invoice", 2 )
tcOrders."Amount Paid" =
roundNumber( tcOrders."Amount Paid", 2 )
tcOrders."Balance Due" =
roundNumber( tcOrders."Balance Due", 2 )
endScan
tcOrders.endEdit()
; For best results; always do this...
if tcOrders.isAssigned() then
tcOrders.close()
endIf
endIf
beep()
Message( "Done!" )
endMethodThe above is taken from a script and assumes that the RoundNumber() method shown earlier has been added as a custom method to the script in question. A copy of the table used in this example is available from our Download section.
include( $DOCUMENT_ROOT . "/lib/pagecomp.php" ); ?>