Round: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
(Add javascript snippet) |
||
Line 5: | Line 5: | ||
Round returns a number that has been rounded to the specified number of decimal places. The required argument, ''number'', is any valid numeric expression. The optional argument, ''fractionaldigits'', is the number of decimal places included in the rounding; if ''fractionaldigits'' is not provided it defaults to 0 and Round returns integers. | Round returns a number that has been rounded to the specified number of decimal places. The required argument, ''number'', is any valid numeric expression. The optional argument, ''fractionaldigits'', is the number of decimal places included in the rounding; if ''fractionaldigits'' is not provided it defaults to 0 and Round returns integers. | ||
== Example == | == Example (Basic) == | ||
<pre> | <pre> | ||
Line 17: | Line 17: | ||
Ate = Round(Sqr(69)) | Ate = Round(Sqr(69)) | ||
Print Ate | Print Ate | ||
</pre> | |||
== Example (JavaScript) == | |||
<pre> | |||
// Round Example | |||
/ *Round rounds numbers to a given decimal place */ | |||
function Round(n,d) { | |||
if (!d || d==null || d=="") {d=0;} | |||
d=Math.floor(d); | |||
d=d<1?0:d; | |||
d=Math.pow(10,d); | |||
var result=Math.round(n*d) / d; | |||
return result; | |||
} | |||
var Pi, Pure, Ate; | |||
Pi = Round(3.14159265, 4); | |||
NSB.Print(Pi); | |||
Pure = Round(99.4444, 2); | |||
NSB.Print(Pure); | |||
Ate = Round(Math.sqrt(69)); | |||
NSB.Print(Ate); | |||
</pre> | </pre> | ||
Revision as of 22:24, 19 May 2013
Round(number[, fractionaldigits])
Description
Round returns a number that has been rounded to the specified number of decimal places. The required argument, number, is any valid numeric expression. The optional argument, fractionaldigits, is the number of decimal places included in the rounding; if fractionaldigits is not provided it defaults to 0 and Round returns integers.
Example (Basic)
Rem Round Example 'Round rounds numbers to a given decimal place Dim Pi, Pure, Ate Pi = Round(3.14159265, 4) Print Pi Pure = Round(99.4444, 2) Print Pure Ate = Round(Sqr(69)) Print Ate
Example (JavaScript)
// Round Example / *Round rounds numbers to a given decimal place */ function Round(n,d) { if (!d || d==null || d=="") {d=0;} d=Math.floor(d); d=d<1?0:d; d=Math.pow(10,d); var result=Math.round(n*d) / d; return result; } var Pi, Pure, Ate; Pi = Round(3.14159265, 4); NSB.Print(Pi); Pure = Round(99.4444, 2); NSB.Print(Pure); Ate = Round(Math.sqrt(69)); NSB.Print(Ate);
Output
3.1416 99.44 8