Data Type Conversion ICS2O.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Types and Arithmetic Operators
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Introduction to Python
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
Unit 3 Lesson 4 How Data Types Affect Calculations Dave Clausen La Cañada High School.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
Mathematical Calculations in Java Mrs. G. Chapman.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Significant Figure Rules RulesExamples The following are always significant Non zero digits Zeros between non zero digits Zero to the right of a non zero.
Mathematical Calculations in Java Mrs. C. Furman.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSCE 102 – Chapter 11 (Performing Calculations) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSC Programming for Science Lecture 5: Actual Programming.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Arithmetic Expressions
Variables, Operators, and Expressions
Significant Figures.
Variables and input/output
Basic Math Skills Workshop
Topics Designing a Program Input, Processing, and Output
Expressions.
Chapter 2 Basic Computation
Chapter 4 Assignment Statement
Operations with Integers
Expressions.
Data Types and Conversions, Input from the Keyboard
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Objective The student will be able to:
Computer Science 3 Hobart College
Chapter 2 Basic Computation
Math in C The math blocks you've used in Scratch can all be recreated in C!
Number and String Operations
Review # 2 Math 8.
Variables ICS2O.
Arithmetic Expressions & Data Conversions
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables Here we go.
Input, Variables, and Mathematical Expressions
Sequences Example Continue the following sequences for the next two terms 1, 2, 3, 4, 5, ……… 2, 4, 6, 8, 10, ………… 1, 3, 5, 7, 9, ………….. 4, 8, 12, 16, ………….
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Python Creating a calculator.
Presentation transcript:

Data Type Conversion ICS2O

Learning Goals Students will: Learn how to convert between two different number types in a mathematical expression. Learn how to convert between text and numerical values.

Type Casting Used when converting between different number types. Ex. Performing a calculation involving integers and storing the final result in a variable of type double int length = 2; double width = 3.7; int area;   area = length * (int)width; System.out.println(“The area of the rectangle is “ + area); Type casting is necessary in the calculation for the area since the variable area has been declared as an int, while width is declared as type double. The (int) is used to cast the double number as an int number.

Truncation When you cast a double to an integer, the value is rounded down to the nearest integer. This is called truncation. Ex. Casting a variable with a value of 3.78 results in a value of 3. This can lead to unexpected results since the value is not rounded according to mathematical rules. You can avoid truncation by adding 0.5 or subtracting 0.5 (for negative numbers) to the variable of type double before type casting. Ex. The calculation for area can be adjusted to round using the following code. area = length * (int)(width + 0.5);

Division Casting becomes very useful when you are doing decimal division of integer values. Dividing two integers will always truncate the value so that the decimal is not preserved. If you cast your int numbers to double numbers, then you are able to preserve the decimal portion. int pizzaSlices = 5; int peopleEating = 2; double slicesPerPerson; slicesPerPerson = pizzaSlices / peopleEating; //integer division; result = 2 slicesPerPerson = (double)pizzaSlices / (double)peopleEating; //decimal division; result = 2.5

Converting from Text to Numeric Data You may also need to convert from text to numeric data. To do this, we use a parse command. String gradeMath = “85”; String gradeScience = “75”; float average; average = (Float.parseFloat(gradeMath) + Float.parseFloat(gradeScience)) / 2; System.out.println("Your average for math and science is " + average); In this example, the grades are stored as strings but are used in a calculation to find the average. The type you wish to convert to has an attribute for this purpose. Float.parseFloat(<string>) Double.parseDouble(<string>) Integer.parseInt(<string>)

Convert from Numeric to String A similar command can be used to convert a numerical value to a string. Float.toString(<number>) Double.toString(<number>) Integer.toString(<number>) Ex. Converting the average value from the previous example. System.out.println("Your average for math and science is " + Float.toString(average));