|
Technical answers from the trenches |
|
Accurate Rounding
| ||||
Posted: 13 October 2000 |
||||
  |
Applies to: All Versions |
|||
  |
Audience: Beginner |
|||
Question: How can I accurately round number (or currency values) to two decimal places?"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". Rounding Numbers in a TableIt'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. |
|||
|
||||||||
|
Copyright © 2000-2004, techtricks.com; All Rights Reserved. Acknowledgements, Disclaimers, Terms and Conditions. |
||||||||
|
Article last updated on 01 June 2003
|
||
|
|
||
|
[- End -]
|