Dealing with Numbers


To assist you in performing the most routine operations, JavaScript provides many ready-made functions you can use directly in your code. There are som many of the functions that we can review only a few.

Converting a Value to an Integer

If you create a form in which a user must type a value, by default, the value the user types is a string, primarily a piece of text. If you want to consider that a number and must involve it in a numerucal expression, you must convert it. To assist you with this, if the value is an integer, JavaScript provides a function named parseInt. The syntax of this function is:

int parseInt(string, radix);

This function takes one required argument and one optional argument. The requirement argument is the value that must be converted:

The second argument is valid only if the first one is, based on the above description. The second argument specifies the base by which the number will be converted. It is a number between 2 and 36. If you don't pass this argument, the number 10 would be used.

Converting a Value to a Floating-Point Number

To let you convert a value to a decimal number with a precision part, JavaScript provides a function namedparseFloat. Its syntax is:

decimal parseFloat(value);

This functakes one argument as the value to be conoverted:

Normally, a floating-point number is made of two sections separated by the character designated by as the desimal separater. If the argument is a number ;ike that, then the function will return it.

Practical LearningPractical Learning: Introducing Built-In Functions

  1. Change the Index.cshtml document as follows:
    <!DOCTYPE html>
    <meta charset="utf-8" />
    <title>ESCAPE: Bill Evaluation</title>
    <link rel="stylesheet" type="text/css" href="CableCompany.css" />
    <div class="container">
        <h1 class="centered">ESCAPE</h1>
        <h2 class="centered">Eastern Shore Cable and Production Entertainment</h2>
        <h3 class="centered">Bill Evaluation</h3>
    <div class="container">
        <form name="BillEvaluation" method="post">
                <legend>Cable TV Services</legend>
                    <label for="CableTVBasicFee">Cable TV Basic Fee:</label>
                    <input type="number" id="CableTVBasicFee" name="CableTVBasicFee" value="0.00" />
                    <label for="DVRService">DVR Service:</label>
                    <input type="number" id="DVRService" name="DVRService" value="0.00" />
                    <label for="SportPackage">Sport Package:</label>
                    <input type="number" id="SportPackage" name="SportPackage" value="0.00" />
                        <input type="button" name="btnEvaluate" value="Evaluate" onclick="evaluatePayment()" />
                    <label for="PaymentAmount">Payment Amount:</label>
                    <input type="number" id="PaymentAmount" name="PaymentAmount" value="0.00" />
        function evaluatePayment() {
            'use strict';
            var basicFee = parseFloat(document.BillEvaluation.CableTVBasicFee.value);
            var dvr = parseFloat(document.BillEvaluation.DVRService.value);
            var sport = parseFloat(document.BillEvaluation.SportsPackage.value);
            var amount = basicFee + dvr + sport;
            document.BillEvaluation.PaymentAmount.value = amount;
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging
  3. Change the values in the text boxes as follows:
    Cabel TV Basic Fee: 24.90
    DVR Service: 10.20
    Sports Package: 9.95
  4. Click the Evaluate button::

    Introduction to Functions

  5. Close the browser and return to your programming environment
  6. Close Microsoft Visual Studio

Previous Copyright © 2018-2019, FunctionX Next