Tag Archives: float

Combining types in output statements in C#

This tutorial shows you how you can combine types in a Console.WriteLine() output statement. For example, you can combine a string with an integer or float value. Watch the video below and scroll down for the sample code.

Sample code

using System;

namespace MyCSharpProject
{
  class MainClass
  {
    public static void Main(string[] args)
    {
      string message = "The result is ";
      float num1 = 6f; // Assign value to num1 variable
      float num2 = 4f; // Assign value to num2 variable

      float result = num1 + num2 + 10f; // Add numbers

      result = num1 - num2; // Subtract
      result = num1 * num2; // Multiply
      result = num1 / num2; // Divide
      result = num1 % num2; // Mod

      int num3 = 30;
      num3--;

      Console.WriteLine(message + result); // String and float value combined in output statement
      Console.WriteLine(num3);
    }
  }
}

Next tutorial: Converting variable types in C#

Data types in Java

A data type classifies various types of data eg. String, integer, float, boolean, the types of accepted values for that data type, operations that can be performed on the data type, the meaning of the data, and the way that data of that type can be stored.

The table below shows the most commonly used data types used in the Java programming language.

Type Description Example
int The integer (int) data type stores positive or negative whole number values. 20
float The float data type stores floating point numbers (numbers with decimal places) eg. 43.65 . Often, you will need to end a float value with an ‘f’ character eg. 43.65f 43.65f
char The char data type stores a single character such as a letter, number, space or special character (eg. a, 1, !).  A char value is always surrounded by single quotes eg. ‘a’. ‘a’
String The String data type stores letters, numbers and other characters in the form of words or sentences. A string value is always surrounded by double quotes eg. “Hello World”. “Hello world”
boolean The boolean data type stores true or false values only eg. true true

The table below shows some of the other data types used in the Java programming language.

Type Description Example
byte The byte data type stores integer numbers ranging from -128 to +127 118
short The short data type is used for integer numbers ranging from -32,768 to +32,767 -28,471
long The long data type is used for integer values exceeding 2.14 billion ‘a’
double The double data type is used for extremely long floating point numbers 1.7976931348623157 × 10^308

Sample code

The sample Java code below shows how some of the different data types can be stored in variables. Later on, we will look at how to actually work with the values of different data types (eg. math calculations with integers and floats, and decision making with booleans).

The code includes comments explaining each data type.

package myjavaproject;

public class DataTypes {
    public static void main (String[] args){
        String message = "Hello"; // variable of String data type
        char letter = 'a'; // variable of char data type
        int number = 20; // variabe of int (integer) data type
        float decimal = 43.65f; // variable of float (floating point) data type
        boolean result = true; // variable of Boolean data type
        
        // now let's output the values of the different variables
        System.out.println("Message is " + message);
        System.out.println("Letter is " + letter);
        System.out.println("Age is " + number);
        System.out.println("Score is " + decimal);
        System.out.println("The answer is " + result);
    }
}

Integers and floats in C#

In the previous tutorial we looked at how to create variables and work with the string data type in C#. In this tutorial we will look at how to work with the integer and float data types in C# and also how to convert strings to integers or floats using the int.Parse() and float.Parse() methods. You will also learn how to manipulate numbers using arithmetic operators.

Watch the video below or scroll down to see the sample code.

Sample C# code

using System;

namespace MyCSharpApp
{
  class MainClass

  {
    public static void Main (string[] args)
    {
      Console.WriteLine ("Enter two numbers...");
      float num1 = float.Parse(Console.ReadLine ());
      float num2 = float.Parse(Console.ReadLine ());
      Console.WriteLine (num1 + num2);
    }
  }
}

Arithmetic operators

Arithmetic operators are used for performing standard math operations on variables and are usually only used number variables (although they can be used for other things too).

Operator Name / description
+ Addition – this operator is used to add two numbers together. It can also be used to concatenate (join) two strings together.
Subtraction – this operator is used to subtract one number from another.
* Multiplication – this operator is used to multiply two numbers together.
/ Division – this operator is used to divide one number by another.
% Modulus – this operator is used to divide one number by another but instead of returning the result, it returns the remainder of the division. Eg. 5%2 would return a result of 1.

Brackets can also be used for more complex math operations eg. 5 + (10 * (6 / 3) / 2);

The assignment operators follow standard mathematic order of operations. That means that the math works from left to right. Parenthesis are done first, multiplication and division comes second, and then addition and subtraction come third.

Assignment operators

Assignment operators are used to assign a value to a variable. The most frequently used assignment operator is the equals (=) sign. There are other operators as well that are used to combine multiple operations into one. The syntax of a standard variable assignment looks like this:

<variable name> <assignment operator> <value>;

For example: x = 5;

The table below shows the different assignment operators available in C#.

Operator Description
= The equals sign is used to assign the value on the right side of the equals sign to the variable on the left side of the equals sign.
+= , -= , *= and /= These assignment operators are also used to perform arithmetic operations and assign the result to the variable eg. x *= 5 is the same as saying x = x * 5.
++ and — These assignment operators are called increment and decrement operators and are used to increase or decrease the value of a variable by 5. For example, x++ is the same as saying x = x + 1.

Next tutorial: Conditional programming in C#

Data types in PHP

This tutorial explains the range of data types that can be used in PHP including:

  • String (letters, numbers and other characters – stored inside quotation marks)
  • Integer (whole positive or negative numbers)
  • Float (positive or negative real numbers – numbers with decimal place)
  • Boolean (can be only one of two possible values – either true or false)
  • NULL (no value / nothing)

String values are always stored inside quotation marks (can be double or single quotation marks, but they must match) eg. $mySentence = “Hello world”;

Integer, float and boolean values are not contained inside quotation marks eg. myNumber = 5;

It is important to note that when working with numbers, if you intend to treat a value as a number (and it will be used for numeric comparisons and calculations) such as a score in a game, then it should be in the form of an integer or float and not stored inside quotation marks. However, if a number value is not going to be used for calculations eg. a phone number or ZIP code, then it can be stored as a string value inside quotation marks.

Watch the video below and scroll down to see the sample code.

PHP sample code:

<?php
 $name = "Batman"; // strings (letters, numbers and other characters)
 $age = 25; // integers (whole positive or negative numbers - no decimal places)
 $num = 30.256; // floats (positive or negative real numbers - can have decimal place)
 $myboolean = true; // Boolean (only one of two possible values - true or false)
 $myvariable = NULL; // NULL (no value / nothing)
?>

PHP Manual references:

Aligning images to left, right and center

In this tutorial, we will look at how to align images using HTML5 and CSS. There is an align attribute for the img tag, however this is not compatible with HTML5 so we will look at aligning images using CSS instead.

Aligning to left or right

If you want to align an image to the left or right, then use the float property (explained in another tutorial here). If you want to align an image to the middle of a page, then read on…

Aligning to middle

If you want to align an image to the middle of a page, then you can use the CSS vertical-align property. You could use it in inline CSS code or in a stylesheet. To align an image to the middle of the page, you give the vertical-align property a value of middle.

If you use the vertical-align property using inline CSS, then it might look like this:

<img src="myimage.png" vertical-align="middle">

If you use the vertical-align property in a stylesheet, then  you might apply the vertical-align property to the img tag or you could give the image a class or ID and then apply the vertical-align property to that class/ID. In a stylesheet, your code might look like this:

img{
   vertical-align: middle;
}

 

Float and clear property

In this tutorial we will use the float and clear CSS properties to arrange images and text on a web page.:

    • float property – with the float property, an element can be pushed to the left or right letting other elements on the web page wrap around it. It is often used to arrange images and for div layouts.
    • clear property – with the clear property, we can specify which side or sides of an element that are floating elements aren’t allowed to be. The float property can be set to none, left, right, both, or inherit (inherit from the parent element). Setting the clear property to ‘both’ for a div means that no other floating divs can be to the left or the right of this div.

In this tutorial, we will float an image and clear a paragraph. We can float an image to the left or right and clear other content around it.

Here is an example of an image floating to the left without clearing the text.

Screen Shot 2015-05-15 at 9.03.35 am

Here is an example of an image floating to the right without clearing the text.

Screen Shot 2015-05-15 at 9.10.24 am

And here is an example of the image floating to the left and the clear property for the paragraph of text set to clear left.

Screen Shot 2015-05-15 at 9.12.44 am

Watch the video below and then scroll down to see the sample HTML and CSS code.

Here is the sample HTML code:

<!DOCTYPE html>
<html>
<head>
 <title>Float and clear</title>
 <link rel="stylesheet" href="resources/stylesheet15.css"/>
</head>
 
<body>
 <img src="resources/canada.jpg" class="pic"/>
 <p class="paragraph">The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.</p>
 
 <p class="paragraph">The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.</p>
</body>
</html>

Here is the sample CSS code:

.pic{
 float:left; /* Float can be left or right */
 padding:5px;
}
.paragraph{
 clear:none; /* clear can be none, left, right, or both */
}

Creating a single-column CSS layout

This tutorial will use what we already know about HTML and CSS from the previous tutorials and introduce a couple of new concepts in order to create a basic layout for a web page.

In this tutorial we will use the following CSS properties that we looked at in the previous tutorial:

      • float property – with the float property, an element can be pushed to the left or right letting other elements on the web page wrap around it. It is often used to arrange images and for div layouts.
      • clear property – with the clear property, we can specify which side or sides of an element that are floating elements aren’t allowed to be. The float property can be set to none, left, right, both, or inherit (inherit from the parent element). Setting the clear property to ‘both’ for a div means that no other floating divs can be to the left or the right of this div.

    For this tutorial we will make a web page with a header, a navigation sidebar (or menu), the main content section, and a footer. The layout will look something like this:

    CSS layout
    The layout contains four divs: header, nav, section, and footer.

    Watch the video below and then scroll down to check out the sample HTML and CSS code.

    Here is the HTML code for the web page. There are four divs with the IDs header, nav, section, and footer. The CSS external stylesheet is in a resources directory and is called theme.css.

    <!DOCTYPE html>
    <html>
    <head>
     <title>Basic CSS Layout</title>
     <meta charset="utf-8"/>
     <link rel="stylesheet" href="resources/theme.css" type="text/css"/>
    </head>
    <body>
     
    <div id="header">
     <h1>My website</h1>
    </div>
     
    <div id="nav">
     <a href="#">Home</a><br>
     <a href="#">About</a><br>
     <a href="#">Products</a><br>
     <a href="#">Support</a><br>
     <a href="#">Contact us</a><br>
    </div>
     
    <div id="section">
     <h2>Welcome to my website</h2>
     <p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
     <p>The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog. The quick brown fox jumped over the lazy dog.</p>
    </div>
     
    <div id="footer">
     Copyright....2015.
    </div>
    </body>
    </html>

    And here is the CSS code in the external stylesheet.

    #header{
     background-color:#0C60CE;
     color:#FFFFFF;
     text-align:center;
     padding:5px;
    }
     
    #nav{
     line-height:25px;
     background-color:#7D9DC7;
     height:300px;
     width:150px;
     float:left;
     padding:5px;
    }
     
    #section{
     width:500px;
     float:left;
     padding:10px;
    }
     
    #footer{
     background-color:#0C60CE;
     color:#FFFFFF;
     clear:both;
     text-align:center;
     padding:20px;
    }

    Tips:

    • You already know how to add background images to a div or body of a web page. Why not use an image for your header div’s background instead? Or try using gradients (something we will look at in a later tutorial).
    • Try adding a sidebar to the right and try using buttons for the menu instead of boring text links.
    • If you don’t know what a div is, then you’ve skipped some tutorials – go back here 😉

    Treating text input from a form as an integer or float

    If you have completed the User Input with Forms tutorial, you will now know how to ask the user for text input through a form, and use that input in some way. However, if you trying collecting input from the user in the form of numbers and then try to do some logical operations on those numbers, you may run into problems.

    The first problem is that any text input will be treated as a string, not an integer. So if you try to compare two numbers eg. check if the inputted number is equal to 5, JavaScript will be trying to check if a string input is equal to a number and will not be checking if an integer input is equal to a specific integer.

    The video and sample code below shows an example of using parseInt() to treat text input from a textbox as an integer value. You can also use parseFloat() to parse a string as a float (real number with decimal places). The video also shows how to grab the value from a textbox by using document.getElementById(‘idname’).value so that a logical operation can happen

    The example used in the video and the code below is a simple math question game. The program displays a math problem to the user. The user can enter their answer in a textbox and the page will display a message if the user gets the correct answer. Watch the video below to see how to write the code for this program.

    Once you have completed the tutorial, your program should work like this:

    MathGame

    Here is the code for the program:

    <html>
    <head>
    <title>Simple math game</title>
    <script type="text/javascript">
    var answer = 15; // this is the answer to the math question
    var attempt; // the user's attempt will be stored in this variable
     
    function guess(){
     attempt = document.getElementById('attempt').value;
     attempt = parseInt(attempt); // this will now treat the textbox input value as an integer
     var result = document.getElementById('result');
     
     if (attempt == answer){
     result.innerHTML = "Correct!";
     }
     else{
     result.innerHTML = "Try again.";
     }
     
    }
     
    </script>
    </head>
    <body>
     <p>What is the answer to 10+5?</p>
     <form>
     Enter your answer:<input type="text" id="attempt"/><br/>
     <input type="button" value="Submit" onclick="guess();"/>
     </form>
     <p id="result">Enter your answer in the textbox above. It must be a number only.</p>
     
     
    </form>
    </body>
    </html>

     Next tutorial: Submitting forms

    Data Types

    This source code shows how create and use variables of different data types. Strings are text (letters, numbers and characters), integers are regular whole numbers (treated as numbers), and floats are real numbers (with decimal places eg. 4.5). You’ll also see how to use the HTML br tag to add a new line between text that is displayed in the browser.

    You can watch the video about variables and data types here.

    <!DOCTYPE html>
    <html lang="en"
    <head>
     <meta charset="utf-8"/>
     <title>JavaScript - Data Types</title>
     <script type="text/javascript">
     //integer
     var age = 20;
     document.write(age);
     
     //float
     var price = 12.95;
     document.write(price);
     
     //string
     var name = "Smith";
     var sentence = "He said \"hello\".";
     
     /*
     Just like in Python, a backslash can be used as an escape character so that
     quotation marks can be used inside a string without any errors
     You can also use single quotes to enclose a string if you want to use double quotes inside a sting
     eg. var sentence = 'He said "hello".';
     */
     
     document.write("</br>", sentence);
     //You can concatenate (join) strings using a comma and you can also use HTML tags inside quotation marks eg. </br>
     //The </br> tag is used to add a new line when the string is displayed in the browser.
     
     </script>
     
    </head>
    <body>
     
     
    </body>
    </html>

    Variables and Data Types in JavaScript

    In this second video from the beginners JavaScript tutorial series, you will learn how to create variables and work with strings, integers, and float data types in JavaScript.

    Variables are used to store information in program such as text, numbers or true/false values. Variables are given a name and are assigned a value. To create a variable in JavaScript we use the var keyword, then specify the variable name, use the = sign to assign a value, and then specify the value to store (you can also create a variable without immediately assigning it a value). For example: var myNumber = 50;

    Variable names must be unique and cannot contain spaces. They don’t begin with a number but can contain numbers. They generally begin with a lowercase letter and if the variable name contains multiple words, then they can be indicated by using camelcase (starting each new word with an uppercase letter) eg. myVariable.

    Variables can store data of different types. The main types are:

    • string – text including letters, numbers and other special characters eg. “Hello world”
    • integer – whole number values eg. 5
    • float – numbers with a decimal point eg. 5.3
    • boolean – a true or false value

    Watch the video below or click here to view it on YouTube.

    The code snippet below shows how to declare variables in JavaScript and how to assign values to a variable. Pay attention to the //comments which explain the different ways of setting up variables.

    <!DOCTYPE html>
    <html lang="en">
    <head>
     <meta charset="utf-8"/>
     <title>JavaScript - Variables</title>
     <script type="text/javascript">
     
     var myVariable = 50;
     
     // Variable names (identifiers) have to begin with lower or uppercase letters or an underscore
     // Variable names cannot begin with a number, however they can contain a number
     // To declare a variable, use the keyword var
     // You can assign a value to a variable when you declare it, in one line (as above)
     // ...or you can declare a variable and then assign a value later, as below...
     
     var myVariable2;
     myVariable2 = "Hello there";
     
     document.write(myVariable);
     // This displays the value of myVariable in the browser window
     
     </script>
     
    </head>
    <body>
     
     
    </body>
    </html>

    This source code shows how create and use variables of different data types. Strings are text (letters, numbers and characters), integers are regular whole numbers (treated as numbers), and floats are real numbers (with decimal places eg. 4.5). Boolean is another data type which is used for true/false values. You’ll also see how to use the HTML br tag to add a new line between text that is displayed in the browser.

    <!DOCTYPE html>
    <html lang="en"
    <head>
     <meta charset="utf-8"/>
     <title>JavaScript - Data Types</title>
     <script type="text/javascript">
     //integer
     var age = 20;
     document.write(age);
     
     //float
     var price = 12.95;
     document.write(price);
     
     //string
     var name = "Smith";
     var sentence = "He said \"hello\".";
     
     /*
     Just like in Python, a backslash can be used as an escape character so that
     quotation marks can be used inside a string without any errors
     You can also use single quotes to enclose a string if you want to use double quotes inside a sting
     eg. var sentence = 'He said "hello".';
     */
     
     document.write("</br>", sentence);
     //You can concatenate (join) strings using a comma and you can also use HTML tags inside quotation marks eg. </br>
     //The </br> tag is used to add a new line when the string is displayed in the browser.
     
     </script>
     
    </head>
    <body>
     
     
    </body>
    </html>

    Hint: Have a look at the toFixed() method if you wish to display floats with a specified number of decimal places.

    Next tutorial: Operators