Download presentation
Presentation is loading. Please wait.
Published byLora Hodge Modified over 9 years ago
1
Copyright ©2005 Department of Computer & Information Science Using Number & Math Objects
2
Copyright ©2005 Department of Computer & Information Science Goals By the end of this lecture you should … Be able to identify and program with intrinsic methods of JavaScript's Number class.Be able to identify and program with intrinsic methods of JavaScript's Number class. Be able to identify and program with intrinsic properties of JavaScript's Math class.Be able to identify and program with intrinsic properties of JavaScript's Math class. Be able to identify and program with intrinsic functions of JavaScript's Math class.Be able to identify and program with intrinsic functions of JavaScript's Math class.
3
Copyright ©2005 Department of Computer & Information Science Number Types JavaScript doesn't distinguish among the different number types. We create new Number objects using the Number constructor method:JavaScript doesn't distinguish among the different number types. We create new Number objects using the Number constructor method: var intNum1 = new Number(5150); var fltNum2 = new Number(867.5309);
4
Copyright ©2005 Department of Computer & Information Science Number.toFixed() Method Number.toFixed() returns a number with a specified number places after the decimal point.Number.toFixed() returns a number with a specified number places after the decimal point. Rounding applies, using these rules:Rounding applies, using these rules: –If a digit is >= 5, then we round up –If a digit is < 5, then we round down If the number has fewer decimal places than the requested decimal places, JavaScript will add zeroes.If the number has fewer decimal places than the requested decimal places, JavaScript will add zeroes.
5
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingNumbers_01.html.
6
Copyright ©2005 Department of Computer & Information Science Number.toString() Method The Number.toString() converts a numeric value to a string type.The Number.toString() converts a numeric value to a string type. Use Number.toString() to output numeric values.Use Number.toString() to output numeric values.
7
Copyright ©2005 Department of Computer & Information Science parseInt() Function parseInt() takes a value and converts that value to a number. Specifically, it converts the value to an integer (whole number).parseInt() takes a value and converts that value to a number. Specifically, it converts the value to an integer (whole number). We can then assign the converted value to a number variable.We can then assign the converted value to a number variable.
8
Copyright ©2005 Department of Computer & Information Science parseFloat() Function parseFloat() takes a value and converts that value to a number. Specifically, it converts the value to a floating point number (number with a fractional value).parseFloat() takes a value and converts that value to a number. Specifically, it converts the value to a floating point number (number with a fractional value). We can then assign the converted value to a number variable.We can then assign the converted value to a number variable.
9
Copyright ©2005 Department of Computer & Information Science isNaN() Function The isNaN() ("not a number") intrinsic function allows us to determine if a value is a numeric type or JavaScript can convert that value to a Number.The isNaN() ("not a number") intrinsic function allows us to determine if a value is a numeric type or JavaScript can convert that value to a Number. Returns true if JavaScript cannot convert to a Number and false if it can convert the value to a Number.Returns true if JavaScript cannot convert to a Number and false if it can convert the value to a Number.
10
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called isNaN_01.html.
11
Copyright ©2005 Department of Computer & Information Science The Math Object JavaScript provides programmers with a convenient way to use common mathematical tasks and properties.JavaScript provides programmers with a convenient way to use common mathematical tasks and properties. JavaScript groups those tasks and properties into the Math object. Unlike String, Number and other classes, we don't need to create and instance of the Math object in order to use its attributes and functions.JavaScript groups those tasks and properties into the Math object. Unlike String, Number and other classes, we don't need to create and instance of the Math object in order to use its attributes and functions.
12
Copyright ©2005 Department of Computer & Information Science Math.PI Property The Math.PI property returns the constant value of Π (Pi).The Math.PI property returns the constant value of Π (Pi). You can use Math.PI for calculating the circumference or area of a circle.You can use Math.PI for calculating the circumference or area of a circle.
13
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_01.html.
14
Copyright ©2005 Department of Computer & Information Science Math.abs() Function The Math.abs() function returns the absolute value of a given value (how far the number is from zero).The Math.abs() function returns the absolute value of a given value (how far the number is from zero).
15
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_02.html.
16
Copyright ©2005 Department of Computer & Information Science Math.ceil() Function The Math.ceil() function returns the smallest integer greater than or equal to a given value.The Math.ceil() function returns the smallest integer greater than or equal to a given value. The method always rounds up, towards 0.The method always rounds up, towards 0.
17
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_03.html.
18
Copyright ©2005 Department of Computer & Information Science Math.floor() Function The Math.floor() function returns the nearest integer less than or equal to a given value.The Math.floor() function returns the nearest integer less than or equal to a given value. The method always rounds down, away from 0.The method always rounds down, away from 0.
19
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_04.html.
20
Copyright ©2005 Department of Computer & Information Science Math.round() Function Rounds a given value to the nearest integer, using the following rules:Rounds a given value to the nearest integer, using the following rules: –If a digit is >= 5, then we round up –If a digit is < 5, then we round down
21
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_05.html.
22
Copyright ©2005 Department of Computer & Information Science Math.max() Function The Math.max() function returns the largest number, given a set of multiple numbers.The Math.max() function returns the largest number, given a set of multiple numbers. Separate the numeric arguments with commas.Separate the numeric arguments with commas.
23
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_06.html.
24
Copyright ©2005 Department of Computer & Information Science Math.min() Function The Math.min() function returns the smallest number, given a set of multiple numbers.The Math.min() function returns the smallest number, given a set of multiple numbers. Separate the numeric arguments with commas.Separate the numeric arguments with commas.
25
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_07.html.
26
Copyright ©2005 Department of Computer & Information Science Math.pow() Function The Math.pow() function returns the result of an exponential expression, given a base and an exponent.The Math.pow() function returns the result of an exponential expression, given a base and an exponent. The base is the first argument and the exponent is the second.The base is the first argument and the exponent is the second.
27
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_08.html.
28
Copyright ©2005 Department of Computer & Information Science Math.sqrt() Function The Math.sqrt() function returns the square root of a given value.The Math.sqrt() function returns the square root of a given value.
29
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_09.html.
30
Copyright ©2005 Department of Computer & Information Science Math.random() Function Math.random() produces a psuedo-random number, between 0 and 1.Math.random() produces a psuedo-random number, between 0 and 1. We usually need to scale the number (multiply by an integer factor) and round it to obtain a whole number.We usually need to scale the number (multiply by an integer factor) and round it to obtain a whole number.
31
Copyright ©2005 Department of Computer & Information Science Take the next few minutes to examine the file called usingMath_10.html.
32
Copyright ©2005 Department of Computer & Information Science Summary JavaScript provides programmers with Number methods to adjust the display of Number data types.JavaScript provides programmers with Number methods to adjust the display of Number data types. JavaScript allows programmers to use Math object properties & functions without instantiating the Math class.JavaScript allows programmers to use Math object properties & functions without instantiating the Math class.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.