JavaScript DHTML/Data Type/Number
Содержание
- 1 Add different type variables together
- 2 Calculate circle area/Peri/Calculate sphere volume
- 3 Calculate the average for integers
- 4 Check if it is a number and calculate the cubic power
- 5 Compare the value for integer numbers
- 6 Convert a Number to a Exponential format
- 7 Convert a number to a precision
- 8 Convert a number to string by append empty string to it
- 9 Convert Fahrenheit to Celsius
- 10 Convert from Celsius to Fahrenheit
- 11 Declare float point number, boolean value and null value
- 12 Fix a number
- 13 Hexadecimal Numbers
- 14 Integer math calculation
- 15 Read a number and compare its value
- 16 Using Binary Flags
Add different type variables together
<source lang="html4strict">
<html> <head> <title></title> </head> <body> <script type="text/javascript"> var num1 = 1; var num2 = 2; var num3 = 3; var fourthvar = "4"; var name1 = "a"; var name2 = "b"; alert(num1 + num2); alert(num3 + fourthvar); alert(name1 + name2); </script> </body> </html>
</source>
Calculate circle area/Peri/Calculate sphere volume
<source lang="html4strict">
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> function calcArea(){
inpRadius = 3; alert(Math.PI * ((inpRadius)*(inpRadius)));
} function calcPeri() {
inpRadius = 3; alert(2 * Math.PI * (inpRadius));
} function calcVol() {
inpRadius = 3; alert(Math.PI * ((inpRadius)*(inpRadius)*(inpRadius)) * (4/3));
} </script> </head> <body> <input type="button" value="Calculate circle area" onclick="calcArea();"> <input type="button" value="Calculate circle Peri" onclick="calcPeri();"> <input type="button" value="Calculate sphere volume" onclick="calcVol();"> </body> </html>
</source>
Calculate the average for integers
<source lang="html4strict">
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> function calcAvg(){
var inpNum1 = 1; var inpNum2 = 2; var inpNum3 = 3; var inpNum4 = 4; numAvg = doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4); alert("The average is: " + numAvg);
} function doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4) {
var ans; ans = (Number(inpNum1) + Number(inpNum2) + Number(inpNum3) + Number(inpNum4)) / 4; return (ans);
} </script> </head> <body onLoad="calcAvg();"> </body> </html>
</source>
Check if it is a number and calculate the cubic power
<source lang="html4strict">
<html> <head> <script type = "text/javascript"> function cubeme(aNum) {
if (aNum == 1) { return 1; } else { return Math.pow(aNum,3); }
} </script> </head> <body> <script type = "text/javascript"> var theNum = 2; var finalNum = cubeme(theNum); if (isNaN(finalNum)) {
alert("Not a number.");
} else {
alert(finalNum);
} </script> </body> </html>
</source>
Compare the value for integer numbers
<source lang="html4strict">
<HTML> <BODY> <SCRIPT language="JavaScript"> var num1 = 0; var num2 = 0; if(num1 == num2) {
window.alert("True");
} else {
window.alert("False");
} </SCRIPT> </BODY> </HTML>
</source>
Convert a Number to a Exponential format
<source lang="html4strict">
<html> <body> <button onclick="var myNum = new Number(100); alert(myNum.toExponential(1));"> Number toExponential </button> </body> </html>
</source>
Convert a number to a precision
<source lang="html4strict">
<html> <body> <button onclick="var myNum = new Number(100); alert(myNum.toPrecision(1));"> Number toPrecision </button> </body> </html>
</source>
Convert a number to string by append empty string to it
<source lang="html4strict">
<html>
<head>
</head>
<body>
<script type="text/javascript">
function numToString(number) { number += ""; return number; } var number = 100; alert(typeof number); number = numToString(number); alert(typeof number);
</script> </body> </html>
</source>
Convert Fahrenheit to Celsius
<source lang="html4strict">
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> function inputFah(){
var fah = 100; ansCel = doCelCalc(fah); alert(fah + " Degrees Fahrenheit is " + ansCel + " Degrees Celsius");
} function doCelCalc(fah){
var ans = ((Number(fah) - 32) / 1.8); return (ans);
} </script> </head> <body>
<input type="button" value="Convert Fahrenheit to Celsius" onClick="inputFah();">
</body> </html>
</source>
Convert from Celsius to Fahrenheit
<source lang="html4strict">
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> function inputCels(){
var cels = 100; ansFah = doFahCalc(cels); alert(cels + " Degrees Celsius is " + ansFah + " Degrees Fahrenheit");
} function doFahCalc(cels){
var ans = ((1.8 * Number(cels)) + 32); return (ans);
} </script> </head> <body> <input type="button" value="Convert Celsius to Fahrenheit" onClick="inputCels();"> </body> </html>
</source>
Declare float point number, boolean value and null value
<source lang="html4strict">
<HTML> <BODY> <SCRIPT language="JavaScript">
</SCRIPT> </BODY> </HTML>
</source>
Fix a number
<source lang="html4strict">
<html> <body> <button onclick="var myNum = new Number(100.22); alert(myNum.toFixed(1));"> Number toFixed </button> </body> </html>
</source>
Hexadecimal Numbers
<source lang="html4strict">
<html> <head> <script type = "text/javascript"> var h = 0xe; var i = 0x2; var j = h * i; document.write(j); </script> </head> </html>
</source>
Integer math calculation
<source lang="html4strict">
<HTML>
<BODY>
<SCRIPT language="JavaScript">
var intValue=200;
document.write(intValue+"
");
intValue += 200;
document.write(intValue+"
");
intValue = intValue-50;
document.write(intValue+"
");
intValue = intValue*0;
document.write(intValue+"
");
intValue = intValue + 500;
document.write(intValue+"
");
intValue -= 80;
document.write(intValue+"
");
</SCRIPT>
</BODY>
</HTML>
</source>
Read a number and compare its value
<source lang="html4strict">
<html> <head>
<title>An If Example</title>
</head> <body> <script type = "text/javascript" > var inputNum = prompt("Please enter a number below 100:"); if (inputNum > 99) {
document.write("That number, " + inputNum + ", is not below 100.");
} </script> </body> </html>
</source>
Using Binary Flags
<source lang="html4strict">
<html> <head> <title>Using Binary Flags</title> <script type="text/javascript"> var FIELD_A = 0x1; // 00001 var FIELD_B = 0x2; // 00010 var FIELD_C = 0x4; // 00100 var FIELD_D = 0x8; // 01000 var FIELD_E = 0x10; // 10000 var fieldsSet = FIELD_A | FIELD_C | FIELD_E; if ((fieldsSet & FIELD_A) && (fieldsSet & FIELD_C)) {
document.write("Fields A and C are set");
} </script> </head> </body> </html>
</source>