Presentation is loading. Please wait.

Presentation is loading. Please wait.

Putting Values into a Suitable Form for Display

Similar presentations


Presentation on theme: "Putting Values into a Suitable Form for Display"— Presentation transcript:

1 Putting Values into a Suitable Form for Display
Formatting Putting Values into a Suitable Form for Display Copyright © Curt Hill

2 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 © Curt Hill

3 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 © Curt Hill

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

5 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 © Curt Hill

6 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 © Curt Hill

7 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 © Curt Hill

8 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 © Curt Hill

9 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 © Curt Hill

10 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 © Curt Hill

11 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 © Curt Hill

12 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 © Curt Hill

13 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 © Curt Hill

14 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 © Curt Hill

15 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 © Curt Hill

16 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 © Curt Hill

17 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 © Curt Hill

18 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 © Curt Hill

19 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 © Curt Hill

20 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 © Curt Hill

21 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 © Curt Hill

22 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 © Curt Hill

23 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 © Curt Hill

24 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 © Curt Hill

25 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 © Curt Hill

26 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 © Curt Hill

27 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 © Curt Hill

28 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 will become 1.235 This is true of all descendents of NumberFormat Copyright © Curt Hill

29 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 © Curt Hill

30 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 © Curt Hill


Download ppt "Putting Values into a Suitable Form for Display"

Similar presentations


Ads by Google