Wojtek`s money/number/dates

Download Report

Transcript Wojtek`s money/number/dates

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.
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)]
Currency Sign
•
•
•
•
•
•
•
•
dollar sign
pound sign
yen sign
yen character
euro sign
thai baht sign
french franc sign
lira sign
•
Source: http://www.xencraft.com/resources/multi-currency.html
U+0024
U+00A3
U+00A5
U+5186
U+20AC
U+0E3F
U+20A3
U+20A4
$
£
¥
円
€
฿
₣
₤
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
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
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
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.
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.
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.
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
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);
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());
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.
Number Formatting Patterns
Symbol
Location
Localized?
0
#
.
,
E
Number
Number
Number
Number
Number
Number
Yes
Yes
Yes
Yes
Yes
Yes
Digit
Digit, zero shows as absent
Decimal separator or monetary decimal separator
Minus sign
Grouping separator
Separates mantissa and exponent in
scientific notation. Need not be quoted in
prefix or suffix.
;
Subpattern boundary
%
Prefix or suffix
\u2030
Prefix or suffix
¤ (\u00A4) Prefix or suffix
Yes
Yes
Yes
No
present in a
separator is used instead
separator.
Prefix or suffix
Separates positive and negative subpatterns
Multiply by 100 and show as percentage
Multiply by 1000 and show as per mille
Currency sign, replaced by currency symbol. If doubled,
replaced by international currency symbol. If
pattern, the monetary decimal
of the decimal
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".
'
Meaning
• 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'* '#'*
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 present
formatter = new DecimalFormat("##");
s = formatter.format(-1234.567);
// -1235
s = formatter.format(0);
// 0
formatter = new DecimalFormat("##00");
s = formatter.format(0);
// 00
•
The . symbol indicates the decimal point
formatter = new DecimalFormat(".00");
s = formatter.format(-.567);
// -.57
formatter = new DecimalFormat(".######");
s = formatter.format(-1234.567);
// -1234.567
formatter = new DecimalFormat("#.000000");
s = formatter.format(-1234.567);
// -1234.567000
Example Patterns Cont.
• The , symbol is used to group numbers
formatter = 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 values
formatter = new DecimalFormat("#;(#)");
s = formatter.format(-1234.567);
// (1235)
• The ' symbol is used to quote literal symbols
formatter = new DecimalFormat("'#'#");
s = formatter.format(-1234.567);
// -#1235
formatter = new DecimalFormat("'abc'#");
s = formatter.format(-1234.567);
// -abc1235
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
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());
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()
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.
Example DateFormat output
DEFAULT
SHORT
MEDIUM
LONG
FULL
U.S. Locale
German Locale
3:58:45 PM
15:58:45
3:58 PM
15:58
3:58:45 PM
15:58:45
3:58:45 PM PDT
15:58:45 GMT+02:00
3:58:45 oclock PM PDT 15.58 Uhr GMT+02:00
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 datetime formatting.
• However, you may modify the format pattern using the
applyPattern methods as desired.
Date Formatting Patterns
Letter
Date or Time Component
G
Era designator
y
Year
M
Month in year
w
Week in year
W
Week in month
D
Day in year
d
Day in month
F
Day of week in month
E
Day in week
a
Am/pm marker
H
Hour in day (0-23)
k
Hour in day (1-24)
K
Hour in am/pm (0-11)
h
Hour in am/pm (1-12)
m
Minute in hour
s
Second in minute
S
Millisecond
z
Time zone
GMT-08:00
Z
Time zone
Presentation
Text
Year
Month
Number
Number
Number
Number
Number
Text
Text
Number
Number
Number
Number
Number
Number
Number
General time zone
Examples
AD [AD / CE or BC/BCE?]
1996; 96
July; Jul; 07
27
2
189
10
2
Tuesday; Tue
PM
0
24
0
12
30
55
978
Pacific Standard Time; PST;
RFC 822 time zone -0800
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"
•
"h:mm a"
•
"hh 'o''clock' a, zzzz"
•
"K:mm a, z"
•
"yyyyy.MMMMM.dd GGG hh:mm aaa"
•
"EEE, d MMM yyyy HH:mm:ss Z"
•
"yyMMddHHmmssZ"
->
-> Wed, Jul 4, '01
12:08 PM
->
-> 12 o'clock PM, Pacific Daylight Time
0:08 PM, PDT
-> 02001.July.04 AD 12:08 PM
-> Wed, 4 Jul 2001 12:08:56 -0700
-> 010704120856-0700
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 string
System.out.print("ENTER DATE STRING: ");
dateString = reader.readLine();
}
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.
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.
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
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
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 birthday
dateOfBirth.add(Calendar.YEAR, age);
// If this year's birthday has not happened yet,
subtract one from age
if (today.before(dateOfBirth))
{
age--;
}
Calendar Examples cont.
•
Before / After
Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);
Calendar newyears = new GregorianCalendar(1999, Calendar.JANUARY, 1);
// Determine which is earlier
boolean b = xmas.after(newyears);
b = xmas.before(newyears);
// false
// true
// Get difference in milliseconds
long diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis();
•
// Get difference in seconds
long diffSecs = diffMillis/(1000);
// 604800
// Get difference in minutes
long diffMins = diffMillis/(60*1000);
// 10080
// Get difference in hours
long diffHours = diffMillis/(60*60*1000);
// 168
// Get difference in days
long diffDays = diffMillis/(24*60*60*1000);
// 7
Note: Is there a better way of doing this?
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
Time Zones
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).
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!
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.
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.
Java Time Zones cont.
• Get the current time in Hong Kong
Calendar 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 GMT
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
int hour24 = cal.get(Calendar.HOUR_OF_DAY); // 0..23
• Get the current local hour-of-day
cal.setTimeZone(TimeZone.getDefault());
hour24 = cal.get(Calendar.HOUR_OF_DAY);
// 0..23
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);