Top Banner
Number Formats Number formats vary in different cultures Group (e.g. thousands) and decimal separators e.g. 1,234.56 (US) vs 1 234,56 (France) Negative numbers e.g. -123 (US) vs 123- (Arabic) Number formats are locale-sensitive. Number formats should be presented correctly for a user’s / customer’s locale.
40

Number Formats

Jan 04, 2016

Download

Documents

tahir iqbal

Number Formats. Number formats vary in different cultures Group (e.g. thousands) and decimal separators e.g. 1,234.56 (US) vs 1 234,56 (France) Negative numbers e.g. -123 (US) vs 123- (Arabic) Number formats are locale-sensitive. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Number Formats

Number Formats

• Number formats vary in different cultures• Group (e.g. thousands) and decimal separators e.g.

1,234.56 (US) vs 1 234,56 (France) • Negative numbers e.g. -123 (US) vs 123- (Arabic)• Number formats are locale-sensitive. • Number formats should be presented correctly for a

user’s / customer’s locale.

Page 2: Number Formats

Currency

• Different name• Different symbol• Fractional currency?

– Dollars, Cents

• Decimal System?– Typically, however…– Mauritania

• Ouguiya is divided into 5 khoum

– Madagascar• Malegasy ariary is divided into 5 iraimbilanja

• Example of the importance of correct currency formats:– Cost of an article of clothing: $123,56 [English (United States)]– Cost of a car: $25.345,99 [Portuguese (Brazil)]

Page 3: Number Formats

Currency Sign

• dollar sign U+0024 $

• pound sign U+00A3 £

• yen sign U+00A5 ¥

• yen character U+5186 円• euro sign U+20AC €

• thai baht sign U+0E3F ฿• french franc sign U+20A3 ₣

• lira sign U+20A4 ₤

• Source: http://www.xencraft.com/resources/multi-currency.html

Page 4: Number Formats

Currency Placement

• Canadian French 123,45 $ – Currency is a suffix

• Canadian English $123.45– Currency is a prefix

• Canadian Inuktitut $123.45– Currency in Nunavut is written same as English.

• Source: http://www.xencraft.com/resources/multi-currency.html

Page 5: Number Formats

Fractional Separators

• Portuguese Escudos 12$34ESC– Fractional separator is dollar sign

• German Deutsch Marks 12,34DM– Fractional separator is comma

• Canadian Dollars (English)$12.34– Fractional separator is full-stop

• Canadian Dollars (French) 12,34 $– Fractional separator is comma

• Swiss Numbers 12'345,67– Fractional separator is comma

• Swiss Francs SFr 12'345.67– Fractional separator is full-stop

• Source: http://www.xencraft.com/resources/multi-currency.html

Page 6: Number Formats

Grouping• British 1,234,567,890.12

– Groups of 3

• Chinese,Japanese 12 億 3456 万 7890.12 or 12 億 3,456 万7,890.12

– Groups of 4, separated by ideographic characters and optionally a thousands separator.

• Indian 1,23,45,67,890.12– One group of 3, then groups of 2

• German 1.234.567.890,12– Groups of 3, separator is full-stop

• Swiss 1'234'567'890,12– Groups of 3, separator is apostrophe

• French 1 234 567 890,12– Groups of 3, separator is a thin non-breaking space

• Source: http://www.xencraft.com/resources/multi-currency.html

Page 7: Number Formats

Other issues

• Rounding– Round up? Round down? – Rounding should follow local conventions– Largely application specific…

• List Separators– 1.23,4.56,7.89– 1,23,4,56,7,89– 1 234 5 678 9 123

• Number Width– Different currencies will require different number width fields.

Page 8: Number Formats

Java Number Formatting

• Java Class NumberFormat• NumberFormat is the abstract base class for all number

formats. • This class provides the interface for formatting and

parsing numbers. • NumberFormat also provides methods for determining

which locales have number formats, and what their names are.

Page 9: Number Formats

Java Number Formatting cont.• static NumberFormat getCurrencyInstance()

– Returns a currency format for the current default locale.• static NumberFormat getCurrencyInstance(Locale inLocale)

– Returns a currency format for the specified locale.• static NumberFormat getInstance()

– Returns the default number format for the current default locale.• static NumberFormat getInstance(Locale inLocale)

– Returns the default number format for the specified locale.• static NumberFormat getIntegerInstance()

– Returns an integer number format for the current default locale.• static NumberFormat getIntegerInstance(Locale inLocale)

– Returns an integer number format for the specified locale.• static NumberFormat getNumberInstance()

– Returns a general-purpose number format for the current default locale.• static NumberFormat getNumberInstance(Locale inLocale)

–  Returns a general-purpose number format for the specified locale.• static NumberFormat getPercentInstance()

– Returns a percentage format for the current default locale.• static NumberFormat getPercentInstance(Locale inLocale)

– Returns a percentage format for the specified locale.

Page 10: Number Formats

Java Number Formatting cont.

• Number to String

NumberFormat nf = NumberFormat.getNumberInstance();

// Or use a specific locale

// getIntegerInstance(Locale inLocale)

double quantity = 12345.678;

String strQuantity = nf.format(quantity);

System.out.println(strQuantity);

12,345.678

Page 11: Number Formats

Java Number Formatting cont.

• String to Number

NumberFormat nf = NumberFormat.getNumberInstance();

// Or use a specific locale

// getIntegerInstance(Locale inLocale)

String strQuantity = 12345.678;

Number num = nf.parse(strQuantity);

Page 12: Number Formats

Java Number Formatting cont.• Alignment, particularly important for currency

int columnWidth = 10;double number = 23321.123;StringBuffer strNumber = new StringBuffer();FieldPosition pos = new

FieldPosition(NumberFormat.INTEGER_FIELD);nf.format(number, strNumber, pos);// Retrieve the updated index to the decimal point.index = pos.getEndIndex();int pad = columnWidth - index;char[] padChars = new char[pad];for (int x = 0; x< pad; x++) {

padChars[x] = ' ';}strNumber.insert(0, padChars);println(strNumber.toString());

Page 13: Number Formats

Java Number Formatting cont.

• Java Class DecimalFormat• DecimalFormat is a concrete subclass of NumberFormat

that formats decimal numbers.• A DecimalFormat comprises a pattern and a set of

symbols. • The pattern may be set directly using applyPattern(), or

indirectly using the API methods. • When using the NumberFormat factory methods, the

pattern and symbols are read from localized ResourceBundles.

Page 14: Number Formats

Number Formatting Patterns

Symbol Location Localized? Meaning

0 Number Yes Digit # Number Yes Digit, zero shows as absent . Number Yes Decimal separator or monetary decimal separator - Number Yes Minus sign , Number Yes Grouping separator E Number Yes Separates mantissa and exponent in

scientific notation. Need not be quoted in prefix or suffix.

; Subpattern boundary Yes Separates positive and negative subpatterns % Prefix or suffix Yes Multiply by 100 and show as percentage \u2030 Prefix or suffix Yes Multiply by 1000 and show as per mille ¤ (\u00A4) Prefix or suffix No Currency sign, replaced by currency symbol. If

doubled, replaced by international currency symbol. If present in a pattern, the monetary decimal separator is used instead of the decimal separator.

' Prefix or suffix No Used to quote special characters in a prefix or suffix, for example, "'#'#" formats 123 to "#123". To create a single quote itself, use two in a row: "# o''clock".

Page 15: Number Formats

• A DecimalFormat pattern contains a positive and negative subpattern.

• Each subpattern has a prefix, numeric part, and suffix.• For example, "#,##0.00;(#,##0.00)".• BNF

pattern := subpattern{;subpattern}

subpattern := {prefix}integer{.fraction}{suffix}

prefix := '\\u0000'..'\\uFFFD' - specialCharacters

suffix := '\\u0000'..'\\uFFFD' - specialCharacters

integer := '#'* '0'* '0'

fraction := '0'* '#'*

Page 16: Number Formats

Example Patterns• The 0 symbol shows a digit or 0 if no digit present

NumberFormat formatter = new DecimalFormat("000000");String s = formatter.format(-1234.567); // -001235

• Notice that the number was rounded up

• The # symbol shows a digit or nothing if no digit presentformatter = new DecimalFormat("##");s = formatter.format(-1234.567); // -1235s = formatter.format(0); // 0formatter = new DecimalFormat("##00");s = formatter.format(0); // 00

• The . symbol indicates the decimal pointformatter = new DecimalFormat(".00");s = formatter.format(-.567); // -.57formatter = new DecimalFormat(".######");s = formatter.format(-1234.567); // -1234.567formatter = new DecimalFormat("#.000000");s = formatter.format(-1234.567); // -1234.567000

Page 17: Number Formats

Example Patterns Cont.

• The , symbol is used to group numbersformatter = new DecimalFormat("#,###,###");

s = formatter.format(-1234.567); // -1,235

s = formatter.format(-1234567.890); // -1,234,568

• The ; symbol is used to specify an alternate pattern for negative valuesformatter = new DecimalFormat("#;(#)");

s = formatter.format(-1234.567); // (1235)

• The ' symbol is used to quote literal symbolsformatter = new DecimalFormat("'#'#");

s = formatter.format(-1234.567); // -#1235

formatter = new DecimalFormat("'abc'#");

s = formatter.format(-1234.567); // -abc1235

Page 18: Number Formats

Dates

• This is based on the "Java Internationalization" book • Dates come in many formats, even in the US:

– 10/4/99; October 4, 1999; 4 Oct 99; etc

• In different languages and locales these are expressed differently – e.g. 4 octobre 1999 in France

• The names of the days and months change • Capitalization rules differ:

– January in English, janvier in French

• Order of components varies: – day/month in Aus, month/day in the US

• What date is this?– 03/04/02

Page 19: Number Formats

Java Date Formatting

• DateFormat class • The standard Java class for handling dates and times in

different locales • The static method DateFormat.getDateInstance() will get

a date format object • The parameters are the date format (short, medium,

long, full) and a locale DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.FRANCE);

• To get a formatted string out of this, pass in a Date object to format() String result = df.format(new Date());

Page 20: Number Formats

Java Date Formatting

• The same class is used to format times using DateFormat.getTimeInstance()

• ... and get both a time and date formatter by DateFormat.getDateTimeInstance()

Page 21: Number Formats

Display length

• SHORT is completely numeric– 12.13.52 or 3:30pm

• MEDIUM is longer– Jan 12, 1952

• LONG is longer still– January 12, 1952 or 3:30:32pm

• FULL is pretty completely specified– Tuesday, April 12, 1952 AD or 3:30:42pm PST.

Page 22: Number Formats

Example DateFormat output

U.S. Locale German Locale

DEFAULT 3:58:45 PM 15:58:45

SHORT 3:58 PM 15:58

MEDIUM 3:58:45 PM 15:58:45

LONG 3:58:45 PM PDT 15:58:45 GMT+02:00

FULL 3:58:45 oclock PM PDT 15.58 Uhr GMT+02:00

Page 23: Number Formats

Java Date Formatting cont.

• Defining your own format • SimpleDateFormat is a concrete class for formatting and

parsing dates in a locale-sensitive manner.• getTimeInstance, getDateInstance, or

getDateTimeInstance in DateFormat provides formatters for default date formatting.

• SimpleDateFormat allows user-defined patterns for date-time formatting.

• However, you may modify the format pattern using the applyPattern methods as desired.

Page 24: Number Formats

Date Formatting PatternsLetter Date or Time Component Presentation Examples G Era designator Text AD [AD / CE or BC/BCE?]y Year Year 1996; 96 M Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day in week Text Tuesday; Tue a Am/pm marker Text PM H Hour in day (0-23) Number 0 k Hour in day (1-24) Number 24 K Hour in am/pm (0-11) Number 0 h Hour in am/pm (1-12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; GMT-

08:00 Z Time zone RFC 822 time zone -0800

Page 25: Number Formats

Date Formatting Patterns

• "yyyy.MM.dd G 'at' HH:mm:ss z“ -> 2001.07.04 AD at 12:08:56 PDT

• "EEE, MMM d, ''yy" -> Wed, Jul 4, '01

• "h:mm a" -> 12:08 PM

• "hh 'o''clock' a, zzzz" -> 12 o'clock PM, Pacific Daylight Time

• "K:mm a, z" -> 0:08 PM, PDT

• "yyyyy.MMMMM.dd GGG hh:mm aaa" -> 02001.July.04 AD 12:08 PM

• "EEE, d MMM yyyy HH:mm:ss Z" -> Wed, 4 Jul 2001 12:08:56 -0700

• "yyMMddHHmmssZ" -> 010704120856-0700

Page 26: Number Formats

Parsing Dates while ((dateString != null) && (dateString.length() > 0)) {

try {

Date date = format.parse(dateString);System.out.println("Original string: " +

dateString);System.out.println("Parsed date : " +

date.toString());System.out.println(); // Skip a line

}catch(ParseException pe) {System.out.println("ERROR: could not parse date in string \"" + dateString + "\"");}// Read another stringSystem.out.print("ENTER DATE STRING: ");dateString = reader.readLine();

}

Page 27: Number Formats

Calendars

• Solar Calendar– Solar calendars assign a date to each solar day. A day may

consist of the period between sunrise and sunset, with a following period of night.

• Julian Calendar– It has a regular year of 365 days divided into 12 months, and a

leap day is added every 4 years, hence the average Julian year is 365.25 days.

– The calendar remained in use into the 20th century in some countries and is still used by many national Orthodox churches.

– Unfortunately, Earth's tropical year is a little less than 365.25 days (it is approximately 365.2422 days), so this calendar, slowly drifted out of sync with the seasons. Hence, the Gregorian calendar was later adopted by most of the West, starting in 1582, and it has since become the world's dominant civic calendar.

Page 28: Number Formats

Calendars cont.

• Gregorian Calendar– Defacto International standard– Lunar calendars– A lunar calendar is a calendar whose date indicates the moon

phase.– Islamic calendar

• A lunisolar calendar is a calendar whose date indicates both the moon phase and the season.– Hebrew calendar– Chinese calendar – Tibetan calendar– and some Hindu calendars.

Page 29: Number Formats

Calendars cont.

• Fiscal calendars– A fiscal calendar fixes each month at a specific number of weeks

to facilitate comparisons from month to month and year to year. – January always has exactly 5 weeks (Sunday through

Saturday), February has 4 weeks, March has 4 weeks, etc.– ISO 8601

Page 30: Number Formats

Java Calendar Class

• A Date object represents a specific instant in time with millisecond precision.

• Whereas, a Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on.

• Subclasses of Calendar interpret a Date according to the rules of a specific calendar system.

• The Java platform provides one concrete subclass of Calendar: GregorianCalendar

Page 31: Number Formats

Calendar Examples• Computing a persons age

Calendar dateOfBirth = new GregorianCalendar(1972, Calendar.JANUARY, 27);

Calendar today = Calendar.getInstance();int age = today.get(Calendar.YEAR) -

dateOfBirth.get(Calendar.YEAR); // Add the tentative age to the date of birth to get

this year's birthdaydateOfBirth.add(Calendar.YEAR, age); // If this year's birthday has not happened yet,

subtract one from ageif (today.before(dateOfBirth)) {

age--;}

Page 32: Number Formats

Calendar Examples cont.• Before / After

Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25); Calendar newyears = new GregorianCalendar(1999, Calendar.JANUARY, 1); // Determine which is earlierboolean b = xmas.after(newyears); // falseb = xmas.before(newyears); // true // Get difference in millisecondslong diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis(); // Get difference in secondslong diffSecs = diffMillis/(1000); // 604800 // Get difference in minuteslong diffMins = diffMillis/(60*1000); // 10080 // Get difference in hourslong diffHours = diffMillis/(60*60*1000); // 168 // Get difference in dayslong diffDays = diffMillis/(24*60*60*1000); // 7

• Note: Is there a better way of doing this?

Page 33: Number Formats

Calendar Examples cont.

• Day of the week• The day-of-week is an integer value where 1 is Sunday,

2 is Monday, ..., and 7 is Saturday

Calendar xmas = new GregorianCalendar(1998, Calendar.JANUARY, 25);

int dayOfWeek = xmas.get(Calendar.DAY_OF_WEEK);

// 6=Friday

Calendar cal = new GregorianCalendar(2003, Calendar.JANUARY, 1);

dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

// 4=Wednesday

Page 34: Number Formats

Time Zones

Page 35: Number Formats

Time Zones cont.

• Solar time. • When the sun reaches its highest point in the sky, it is noon.

Different cities had different times…• As telecommunications improved and with the expansion of the

railways this became increasingly awkward.• Time zones are generally centered on meridians of a longitude that

is a multiple of 15° thus making neighboring time zones one hour apart.

• However, the one hour separation is not universal and, as the map demonstrates.

• All time zones are defined relative to Coordinated Universal Time (UTC).

• The reference point for Time Zones is the Prime Meridian (longitude 0°) which passes through the Royal Greenwich Observatory in Greenwich, London, England.

• Greenwich Mean Time (GMT).

Page 36: Number Formats

Time Zones cont.

• Los Angeles, California, USA: UTC − 8 – (e.g. if it is 12:00 UTC, then it is 04:00 in Los Angeles)

• Toronto, Ontario, Canada: UTC − 5 – (e.g. if it is 11:00 UTC, then it is 06:00 in Toronto)

• Stockholm, Sweden: UTC + 1 – (e.g. if it is 12:00 UTC, then it is 13:00 in Stockholm)

• Melbourne, Japan: UTC + 10 – (e.g. if it is 11:00 UTC, then it is 21:00 in Tokyo)

• Note: The time zone adjustment for a specific location may vary due to the use of daylight saving time.

• e.g. New Zealand which is usually UTC + 12, observes a one-hour daylight saving time adjustment during the southern hemisphere summer resulting in a local time of UTC + 13!

Page 37: Number Formats

Daylight savings

• Pros– Energy conservation– “United States Department of Transportation studies showed

that DST reduces the country's electricity usage by one percent during each day DST is in effect.”

– Increased opportunities for outdoor activities & decreased crime.

• Cons– Agriculture – the cows don’t follow it.

Page 38: Number Formats

Java Time Zones

• Java Class TimeZone• TimeZone represents a time zone offset, and also

figures out daylight savings.• SimpleTimeZone is a concrete subclass of TimeZone

that represents a time zone for use with a Gregorian calendar.

• The class holds an offset from GMT, called raw offset, and start and end rules for a daylight saving time schedule.

Page 39: Number Formats

Java Time Zones cont.

• Get the current time in Hong KongCalendar cal = new

GregorianCalendar(TimeZone.getTimeZone("Hongkong"));

int hour12 = cal.get(Calendar.HOUR); // 0..11

int minutes = cal.get(Calendar.MINUTE); // 0..59

int seconds = cal.get(Calendar.SECOND); // 0..59

boolean am = cal.get(Calendar.AM_PM) == Calendar.AM;

• Get the current hour-of-day at GMTcal.setTimeZone(TimeZone.getTimeZone("GMT"));

int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23

• Get the current local hour-of-daycal.setTimeZone(TimeZone.getDefault());

hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23

Page 40: Number Formats

Java Time Zones cont.

• Does the time zone have a daylight savings time period?boolean hasDST = tz.useDaylightTime();

• Is the time zone currently in a daylight savings time?boolean inDST = tz.inDaylightTime(today);