Prototype.js - SPLessons

Prototype.js Numbers

Home > Lesson > Chapter 5
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Prototype.js Numbers

Prototype.js Numbers

shape Introduction

This chapter demonstrates about the Prototype.js Numbers is used to extension to the built in Number object and following are the concepts are covered in this chapter.
  • Numbers & Number Methods

Numbers & Number Methods

shape Description

Prototype extends native java script numbers which is used to provide some functionalities as shown below.
  • ObjectRange compatibility through Number#succ.
  • Numerical Loops with Number#times.
  • Some Simple utility methods like Number#ToColorPart and Number#toPaddedSString.
Prototype number has several methods in which some methods are briefly explained below. abs() abs defines the absolute, which returns the absolute value of the number and it is a convincing method which simply calls the Math.abs on instance and returns the results. The code below demonstrates the abs() method as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { alert( "Math.abs(-6) : " + Math.abs(-6) ); alert( "(-5).abs() : " + (-6).abs() ); alert( "(5).abs() : " + (6).abs() ); } </script> </head> <body> <p>Click the button to see the result.</p> <br /> <br /> <input type = "button" value = "submit" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. When user clicked on the submit button then user will get the result as shown below image. ceil ceil returns the smallest integer which is greater then or equal to the number which simply called as  Math.Ceil then it returns the result. The code below demonstrates the ceil as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { alert( "Math.ceil(5.1) : " + Math.ceil(5.1) ); alert( "(5.1).ceil() : " + (5.1).ceil() ); alert( "(-5.1).ceil() : " + (-5.1).ceil() ); } </script> </head> <body> <p>Click the button to get the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. When user clicked on the result button then user will get the result as shown below image. floor floor returns the largest integer less than or equal to the number which simply called as Math.Floor then it returns the result. The code below demonstrates the floor as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { alert( "Math.floor(5.6) : " + Math.floor(5.6) ); alert( "(5.6).floor() : " + (5.6).floor() ); alert( "(-5.1).floor() : " + (-5.1).floor() ); } </script> </head> <body> <p>Click the button to get the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. When user clicked on the result button then user will get the following result as shown below image. round round rounds the number to the nearest integer value which simply called as Math.round then it returns the result. The code below demonstrates the round as shown below. [jscript] <html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> function showResult() { alert( "Math.round(5.5) : " + Math.round(5.5) ); alert( "(5.5).round() : " + (5.5).round() ); alert( "(5.49).round() : " + (5.49).round() ); alert( "(-5.5).round() : " + (-5.5).round() ); } </script> </head> <body> <p>Click the button to get the result.</p> <br /> <br /> <input type = "button" value = "Result" onclick = "showResult();"/> </body> </html> [/jscript] Result By running the above code in a preferred browser user can get the following output as shown in below image. When user clicked on the result button then user will get the result as shown below image.

Summary

shape Key Points

  • abs returns the absolute value.
  • Round rounds the number to the nearest integer value.