This is an old revision of the document!
by Richard Russell, June 2016
If you want to round a numeric value to a specific number of decimal places or significant figures there are a number of ways in which this can be achieved. The most flexible way is to use the @% system variable because, when used in conjunction with PRINT or the STR$() function, it provides the greatest degree of control over the format of the resulting number. However at the same time it is probably the least user-friendly method.
Another approach is to use the FNusing library function. This provides a functionality very similar to the PRINT USING statement in some other BASICs, but with the advantage that it can be used not only when PRINTing the number but also when writing it to a string variable or to a file etc.
Yet another method is to take advantage of one or other of the user-defined functions listed below. The first formats a number to the specified number of decimal places and the second formats it to the specified number of significant figures:
DEF FNround_dp(n, dp%) LOCAL @% @% = &1420000 + dp% * 256 = STR$(n)
DEF FNround_sf(n, sf%) LOCAL @% @% = &1000000 + sf% * 256 = STR$(n)
It is perhaps worth emphasising that the concept of rounding to a certain number of digits applies only to decimal numbers, which is why the above functions return strings. BBC BASIC holds numeric values internally in binary, so you cannot meaningfully refer to them as being rounded to a certain number of decimal digits.