Fix: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
(Add javascript snippet)
Line 5: Line 5:
Fix removes the fractional part from a number, returning the integer closest to 0. The required parameter, ''number'', is any numeric expression. When ''number'' is positive, the next smallest integer is returned; when ''number'' is negative, the next largest integer is returned. The required parameter, ''number'', is any valid numeric expression.
Fix removes the fractional part from a number, returning the integer closest to 0. The required parameter, ''number'', is any numeric expression. When ''number'' is positive, the next smallest integer is returned; when ''number'' is negative, the next largest integer is returned. The required parameter, ''number'', is any valid numeric expression.


== Example ==
== Example (Basic) ==


<pre>
<pre>
Line 15: Line 15:
Print "Fix(e) = " & Fix(Pos)
Print "Fix(e) = " & Fix(Pos)
Print "Fix(-e) = " & Fix(Neg)
Print "Fix(-e) = " & Fix(Neg)
</pre>
== Example (JavaScript) ==
<pre>
// Fix Example
/* Fix converts floats to int nearest 0 */
Fix = function(n) {
  var i=Math.floor(n);
  if (i<0) { i++; };
  return i*1;
}
var Pos, Neg;
Pos = Math.exp(1);
Neg = -Math.exp(1);
NSB.Print("Fix(e) = " + Fix(Pos));
NSB.Print("Fix(-e) = " + Fix(Neg));
</pre>
</pre>



Revision as of 19:19, 19 May 2013

Fix(number)

Description

Fix removes the fractional part from a number, returning the integer closest to 0. The required parameter, number, is any numeric expression. When number is positive, the next smallest integer is returned; when number is negative, the next largest integer is returned. The required parameter, number, is any valid numeric expression.

Example (Basic)

Rem Fix Example
'Fix converts floats to int nearest 0
Dim Pos, Neg
Pos = Exp(1)
Neg = -Exp(1)
Print "Fix(e) = " & Fix(Pos)
Print "Fix(-e) = " & Fix(Neg)

Example (JavaScript)

// Fix Example
/* Fix converts floats to int nearest 0 */

Fix = function(n) {
  var i=Math.floor(n);
  if (i<0) { i++; };
  return i*1;
}

var Pos, Neg;
Pos = Math.exp(1);
Neg = -Math.exp(1);
NSB.Print("Fix(e) = " + Fix(Pos));
NSB.Print("Fix(-e) = " + Fix(Neg));

Output

Fix(e) = 2
Fix(-e) = -2

Related Items

Int