User Tools

Site Tools


rounding_20functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

rounding_20functions [2018/03/31 13:19]
127.0.0.1 external edit
rounding_20functions [2018/04/17 18:34] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //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 [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​atpercent|@% 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 [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​using|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**:​\\ \\  //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 [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​atpercent|@% 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 [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​using|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**:​\\ \\ 
 +<code bb4w>
         DEF FNround_dp(n,​ dp%)         DEF FNround_dp(n,​ dp%)
         LOCAL @%         LOCAL @%
Line 11: Line 12:
         @% = &​1000000 + sf% * 256         @% = &​1000000 + sf% * 256
         = STR$(n)         = STR$(n)
 +</​code>​
 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. 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.
rounding_20functions.txt ยท Last modified: 2018/04/17 18:34 by tbest3112