Explanation of Rounding & Truncating in Javascript

JavaScript represents a foundational scripting language for the web. Within JavaScript, web programmers find a fully functional object-oriented scripting language with numerous built-in capabilities. Many of these capabilities come from the "Math" object and the computations it allows programmers to perform. Through the Math object, programmers can round numbers traditionally, or use the "floor()" and "ceil()" functions to perform more specialized rounding operations. Through these functions, the programmer can also round or truncate decimals to an arbitrary precision.

  1. Rounding in JavaScript

    • JavaScript uses the "round()" function, packaged as part of the "math" object, to round decimal fractions to the nearest integer. This operation will always return an integer, with no decimal parts. In order to round to a specific decimal, the programmer can include the round() function in the following equation, where n = the number to round and t the number of decimal places to round to:

      (round(n * 10^t)) / 10^t

      For example, to round the number 4.543 to two decimal places, the JavaScript command would look like this:

      (Math.round(n*100) / 100

    Rounding Using the "floor()" and "ceil()" Functions

    • Another way to round in JavaScript involves two other Math object functions, the "floor()" and "ceil()" (ceiling) functions. The ceil() function rounds to the nearest integer towards positive infinity, regardless of the decimal part. So, 3.1 will round toward 4, and -3.9 will round to 3. The floor() function rounds to the nearest integer towards negative infinity. So, 3.9 will round to 3, and -3.1 will round to -4.

    Truncating

    • "Truncating" a number means dropping the fractional part of a decimal number. A truncation operation does not round a number; it simply drops the fractional portion of the decimal number. In other programming languages where variable data type is required upon variable declaration, such as C++, this usually occurs where a decimal number converts to an integer. So, 3.5 truncated results in the integer 3, as does 3.45933544. Converting between two data types, such as a decimal to an integer, results in a truncation of the fractional parts, as in the following example:

      float x = 4.5634;

      int y = (int)x; //y = 4

    Truncating in JavaScript

    • Unlike C++, JavaScript variables do not require a type declaration. Therefore, the programmer can use rounding to simulate a truncate operation. With the floor() and ceil() functions, the programmer can use a simple "if" statement to truncate a number regardless of its value. If the value is positive, the floor() function will truncate it. If it is negative, the ceil() function will do the same thing. This following example will truncate both positive and negative decimals:

      var n = 4.5321;

      if (n > 0){

      n = Math.floor(n);

      }

      else if (n < 0){

      n = Math.ceil(n);

      }

    Truncating to a Decimal Place

    • Using this truncating technique, the programmer can also remove excess decimal places after a certain position. If the programmer wishes to drop all decimal places after the second decimal without rounding, the floor() and ceil() functions will work as part of the precision rounding formula "[floor(n) or ceil(n)]as in the following example:

      var n = 5.69483;

      if (n > 0){

Related Searches:

References

Comments

Related Ads

Featured