Number formats
This presentation is the property of its rightful owner.
Sponsored Links
1 / 40

Number Formats PowerPoint PPT Presentation


  • 103 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

Number Formats

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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.


Currency

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

Currency Sign

  • dollar signU+0024$

  • pound signU+00A3£

  • yen signU+00A5¥

  • yen characterU+5186円

  • euro signU+20AC€

  • thai baht signU+0E3F฿

  • french franc signU+20A3₣

  • lira signU+20A4₤

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


Currency placement

Currency Placement

  • Canadian French123,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

Fractional Separators

  • Portuguese Escudos12$34ESC

    • Fractional separator is dollar sign

  • German Deutsch Marks12,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 Numbers12'345,67

    • Fractional separator is comma

  • Swiss FrancsSFr 12'345.67

    • Fractional separator is full-stop

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


Grouping

Grouping

  • British1,234,567,890.12

    • Groups of 3

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

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

  • Indian1,23,45,67,890.12

    • One group of 3, then groups of 2

  • German1.234.567.890,12

    • Groups of 3, separator is full-stop

  • Swiss1'234'567'890,12

    • Groups of 3, separator is apostrophe

  • French1 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

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 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

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 cont1

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 cont2

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 cont3

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 cont4

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

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".


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'* '#'*


Example patterns

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

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

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

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 formatting1

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

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

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


Java date formatting cont

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.


Date formatting patterns

Date Formatting Patterns

Letter Date or Time Component Presentation Examples

G Era designator TextAD [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


Date formatting patterns1

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


Parsing dates

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

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

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 cont1

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

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

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

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); // false

    b = xmas.before(newyears); // 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 cont1

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


Time zones cont

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 cont1

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

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 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

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 cont1

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);


  • Login