Putting Values into a Suitable Form for Display

Slides:



Advertisements
Similar presentations
Chapter 2 Screen Output Section 2.1 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Advertisements

11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
Chapter 5 Using Data and COBOL Operators. Initializing Variables When you define a variable in WORKING- STORAGE, you also can assign it an initial value.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Chapter 2 Variables.
Copyright © – Curt Hill Types What they do.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Copyright © Curt Hill Formatting Reals Outputs other than normal.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
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.
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
CHAPTER 5: Representing Numerical Data
Floating Point Numbers
28 Formatted Output.
The Second C++ Program Variables, Types, I/O Animation!
More About Objects and Methods
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Chapter 2: Introduction to C++
Outline Creating Objects The String Class Packages Formatting Output
More important details More fun Part 3
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Tokens in C Keywords Identifiers Constants
Chapter 3: Using Methods, Classes, and Objects
What are they? Why do we need them?
Chapter 3 Java Input/Output.
Chapter 2 Screen Output Section 2.1
Chapter 2.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Defining Your Own Classes
INPUT & OUTPUT scanf & printf.
Introduction to Java, and DrJava part 1
Chapter 1: Computer Systems
Arrays in Java What, why and how Copyright Curt Hill.
Chapter 2 Variables.
PHP.
C++ Data Types Data Type
Lectures on Numerical Methods
Chapter 3 Input/Output.
Chapter 35 Internationalization
Storing Negative Integers
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Topics Designing a Program Input, Processing, and Output
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
What are they? Why do we need them?
Introduction to Java, and DrJava
Expressions and Assignment
Conversion Check your class notes and given examples at class.
Chapter 2: Introduction to C++.
The Java switch Statement
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Defining Classes and Methods
Floating Point Numbers
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Introduction to C Programming
Varying Character Lengths
Presentation transcript:

Putting Values into a Suitable Form for Display Formatting Putting Values into a Suitable Form for Display Copyright © 2006-2018 Curt Hill

The Problem Java uses types int and double to represent numeric values These have a binary internal representation People want to see a character representation in a report or in a window There are many such representations How do we display these? Copyright © 2006-2018 Curt Hill

What do we want? Although the basic digits are the same we may want to see other information Commas separating groups of digits Leading/trailing zero suppression Currency symbols such as $ Handling of the sign other than a leading + / - Such as CR or DB We call this editing Copyright © 2006-2018 Curt Hill

If that is not bad enough Java is an international language The problem is complicated by different locales Do you want: 341,450.78 – Common in America 341.450,78 – Common in Europe 341 450,78 – Also used in Europe Just as most Operating Systems keep track of this, Java does as well Copyright © 2006-2018 Curt Hill

The Problem Revisited The Java compiler is notoriously poor at guessing the intent of the programmer So the default formatting is usually insufficient We will need to specify more information to get what we want This will involve new classes and new methods Copyright © 2006-2018 Curt Hill

NumberFormat We will use a NumberFormat object to format numbers into strings It has several methods that will be of interest The format method will either take a long int or a double An int will automatically cast into a long as also a float into a double This method will return a string which is the formatted number Copyright © 2006-2018 Curt Hill

NumberFormat NumberFormat is an abstract base class It is the ancestor of several classes that will be important The actual class will determine the formatting As an abstract base class it cannot be instantiated What does that mean? Copyright © 2006-2018 Curt Hill

Abstract Base Classes NumberFormat is an abstract base class Does that mean we cannot: NumberFormat nf; No It means that we cannot: nf = new NumberFormat(…); The variable nf may hold any descendent of NumberFormat Copyright © 2006-2018 Curt Hill

NumberFormat Descendents The currency classes are simple but not too simple DecimalFormat is the most general and complicated Before dealing with the currency descendents and instantiating an abstract base class we need to consider a Locale object Copyright © 2006-2018 Curt Hill

Locale A Locale is an object that represents a region This captures both geography and language Used to modify how things are displayed Specifically here currency symbols and format of numbers Create a Locale object if you do not want your own locale Copyright © 2006-2018 Curt Hill

Locale Constructors The constructor may specify a language, nation or system Locale loc = new Locale(“en”); English, default country Locale loc = new Locale(“en”,”GB”); English of Great Britain Locale loc = new Locale(“en”,”US”,”MAC”); English, USA for a MacIntosh system Copyright © 2006-2018 Curt Hill

Alternative Often it is easier to use static Locales Locale loc = Locale.UK; instead of previous Locale loc = Locale.CANADA; English for Canadians Locale loc = Locale.CANADA_FRENCH; French for Canadians Copyright © 2006-2018 Curt Hill

Abstract Base Classes Again Suppose NumberFormat nf; We cannot use: nf = new NumberFormat(…); How then do we? We use the NumberFormat method getCurrencyInstance to make a descendent of NumberFormat that is instantiable Copyright © 2006-2018 Curt Hill

Currencies There are several descendents of NumberFormat that represent the currencies of various locales The NumberFormat static method getCurrencyInstance is used to obtain If no parameter then get the Locale for this machine: nf = NumberFormat. getCurrencyInstance(); Copyright © 2006-2018 Curt Hill

Formatting for Currency We now know the process: Declare a NumberFormat object Initialize with getCurrencyInstance Use format method to create the string Now look at the example code: Copyright © 2006-2018 Curt Hill

Example double d; … // load d with value NumberFormat nf; nf = nf.getCurrencyInstance(); String s = nf.format(d); … // display s somewhere The nf before getCurrencyInstance could have been spelled out as NumberFormat, since this is a static method Copyright © 2006-2018 Curt Hill

getCurrencyInstance Has two signatures One without parameters One with a Locale object as a parameter Use the latter to display currencies different than the local one See example Copyright © 2006-2018 Curt Hill

Example double d; … // load d with value NumberFormat nf; Locale loc = Locale.UK; nf = nf.getCurrencyInstance(loc); String s = nf.format(d); … // display s somewhere Now a pound symbol will be displayed instead of a dollar sign Copyright © 2006-2018 Curt Hill

Not yet enough It is one thing to put us in correct locale, it is another thing to use the right currency symbol In London they are just as likely to accept a Euro or Dollar as a Pound How is formatting for Euros in London different than formatting Euros in France? The formatting is based on Locale, but other currency symbols can be used Copyright © 2006-2018 Curt Hill

Currency A class that describes what symbol to use in currency formatting We will use a static method to create, somewhat like NumberFormat We can create one with a three character abbreviation Currency newCurrency = Currency.getInstance("EUR"); Copyright © 2006-2018 Curt Hill

Currency continued There are several abbreviations that are usable: USD = US Dollar HKD = Hong Kong Dollar EUR = Euro Use setCurrency to establish that in a NumberFormat object See the next example Copyright © 2006-2018 Curt Hill

Formatting Euros in England Currency newCurrency = Currency.getInstance("EUR"); NumberFormat nf; Locale loc = Locale.UK; nf = nf.getCurrencyInstance(loc); nf.setCurrency(newCurrency); String s = nf.format(d); … // display s somewhere Copyright © 2006-2018 Curt Hill

There is always more Formatting a number as currency is an important task There are many other ways to format a number How do we do this? Use the DecimalFormat object This generalizes most any type of formatting Copyright © 2006-2018 Curt Hill

DecimalFormat Descendent of NumberFormat Will use the format method Uses a pattern to edit the text This pattern is similar to many other numeric editing patterns The PIC clause in COBOL Numerous others The pattern is just a string of characters Copyright © 2006-2018 Curt Hill

Pattern Format Just a string Each character in string is either an editing character or will appear as-is Editing characters have a special meaning for the formatting The semicolon divides the pattern into two pieces First is how to handle if number is positive The second is for negatives Copyright © 2006-2018 Curt Hill

Pattern Characters # means a digit, with leading zero suppression 0 means a digit that always displays . Decimal separator - Minus sign , Grouping separator E Separates mantissa and exponent in scientific notation % Multiply by 100 and show as percentage ' Used to quote special characters in a prefix or suffix "'#'#" formats 123 to "#123“ Copyright © 2006-2018 Curt Hill

Suppression Only certain values may exist within the number Such as the comma These will also be suppressed just like a digit The result string may be shorter than the pattern Any characters may be outside the number representation string They will display as themselves Copyright © 2006-2018 Curt Hill

Rounding When a float or double is cast as an int truncation occurs All digits to right of decimal are lost Formatting always rounds Not to the nearest integer To nearest displayable number Thus using “###.###” the number 1.23461 will become 1.235 This is true of all descendents of NumberFormat Copyright © 2006-2018 Curt Hill

Imports Several new import statements are needed for this import java.text.NumberFormat; import java.text.DecimalFormat; import java.util.Locale; import java.util.Currency; Copyright © 2006-2018 Curt Hill

Conclusion There are simple and more complicated ways to make the number look the way we would like NumberFormat with getCurrencyInstance is the simplest way for currency displays DecimalFormat is the most general and should be able to do any display Copyright © 2006-2018 Curt Hill