How to Perform Addition of Nonrepeating Numbers in Javascript

Nonrepeating numbers such as pi are decimals that have no solution. You must round the number in JavaScript to perform addition on the values. JavaScript includes the "Round" function that rounds the number to a specified decimal place. After the decimal is rounded, you can perform addition on the number without memory-overflow errors.

Instructions

    • 1

      Right-click the HTML or JS file that contains the JavaScript you want to edit. Click "Open With," then click your preferred HTML editor in the popup menu.

    • 2

      Create two fractions using the "Round" function. The following code shows you how to create fractional decimal numbers in JavaScript.

      var num1 = Math.Round(1.3895983475, 2);

      var num2 = Math.Round(3.9874938, 2);

      Notice both fractions are nonrepeating decimal numbers. The Round function rounds both of these numbers to the second decimal place. Therefore, the two numbers returned are "1.39" and "3.99."

    • 3

      Add the two numbers and assign the result to a third variable. The following code shows you how to add the numbers and assign the "5.38" result to the variable "result":

      var result = var1+var2;

    • 4

      Print the result to the Web page. The following "document.write" command prints the result to the Web page:

      document.write(result);

Related Searches:

References

Comments

Related Ads

Featured