Number Formats - PowerPoint PPT Presentation

About This Presentation
Title:

Number Formats

Description:

Chinese,Japanese 12? 3456? 7890.12 or 12? 3,456? 7,890.12 ... The ' symbol is used to quote literal symbols. formatter = new DecimalFormat ... – PowerPoint PPT presentation

Number of Views:226
Avg rating:3.0/5.0
Slides: 41
Provided by: janNew
Category:

less

Transcript and Presenter's Notes

Title: Number Formats


1
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 users / customers locale.

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

3
Currency Sign
  • dollar sign U0024
  • pound sign U00A3
  • yen sign U00A5
  • yen character U5186 ?
  • euro sign U20AC
  • thai baht sign U0E3F ?
  • french franc sign U20A3 ?
  • lira sign U20A4
  • Source http//www.xencraft.com/resources/multi-cu
    rrency.html

4
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-cu
    rrency.html

5
Fractional Separators
  • Portuguese Escudos 1234ESC
  • 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-cu
    rrency.html

6
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-cu
    rrency.html

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

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

9
Java Number Formatting cont.
  • static NumberFormat getCurrencyInstance()
  • Returns a currency format for the current default
    locale.
  • static NumberFormat getCurrencyInstance(Locale inL
    ocale)
  • 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 inLo
    cale)
  • 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 inLoc
    ale)
  •  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 inLo
    cale)

10
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

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

12
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 charpad
  • for (int x 0 xlt pad x)
  • padCharsx ' '
  • strNumber.insert(0, padChars)
  • println(strNumber.toString())

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

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

15
  • 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 subpatternsubpattern
  • subpattern prefixinteger.fractionsuffix
  • prefix '\\u0000'..'\\uFFFD' -
    specialCharacters
  • suffix '\\u0000'..'\\uFFFD' -
    specialCharacters
  • integer '' '0' '0'
  • fraction '0' ''

16
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

17
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

18
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

19
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(DateFor
    mat.FULL, Locale.FRANCE)
  • To get a formatted string out of this, pass in a
    Date object to format()
  • String result df.format(new Date())

20
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()

21
Display length
  • SHORT is completely numeric
  • 12.13.52 or 330pm
  • MEDIUM is longer
  • Jan 12, 1952
  • LONG is longer still
  • January 12, 1952 or 33032pm
  • FULL is pretty completely specified
  • Tuesday, April 12, 1952 AD or 33042pm PST.

22
Example DateFormat output
  • U.S. Locale German Locale
  • DEFAULT 35845 PM 155845
  • SHORT 358 PM 1558
  • MEDIUM 35845 PM 155845
  • LONG 35845 PM PDT 155845 GMT0200
  • FULL 35845 oclock PM PDT 15.58 Uhr GMT0200

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

24
Date Formatting Patterns
  • Letter 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-0800

25
Date Formatting Patterns
  • "yyyy.MM.dd G 'at' HHmmss z -gt 2001.07.04
    AD at 120856 PDT
  • "EEE, MMM d, ''yy" -gt Wed, Jul 4, '01
  • "hmm a" -gt 1208 PM
  • "hh 'o''clock' a, zzzz" -gt 12 o'clock PM,
    Pacific Daylight Time
  • "Kmm a, z" -gt 008 PM, PDT
  • "yyyyy.MMMMM.dd GGG hhmm aaa" -gt
    02001.July.04 AD 1208 PM
  • "EEE, d MMM yyyy HHmmss Z" -gt Wed, 4 Jul
    2001 120856 -0700
  • "yyMMddHHmmssZ" -gt 010704120856-0700

26
Parsing Dates
  • while ((dateString ! null)
    (dateString.length() gt 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()

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

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

29
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

30
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

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

32
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/(601000) //
    10080

33
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)
  • // 6Friday
  • Calendar cal new GregorianCalendar(2003,
    Calendar.JANUARY, 1)
  • dayOfWeek cal.get(Calendar.DAY_OF_WEEK)
  • // 4Wednesday

34
Time Zones
35
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).

36
Time Zones cont.
  • Los Angeles, California, USA UTC - 8
  • (e.g. if it is 1200 UTC, then it is 0400 in Los
    Angeles)
  • Toronto, Ontario, Canada UTC - 5
  • (e.g. if it is 1100 UTC, then it is 0600 in
    Toronto)
  • Stockholm, Sweden UTC 1
  • (e.g. if it is 1200 UTC, then it is 1300 in
    Stockholm)
  • Melbourne, Japan UTC 10
  • (e.g. if it is 1100 UTC, then it is 2100 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!

37
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 dont follow it.

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

39
Java Time Zones cont.
  • Get the current time in Hong Kong
  • Calendar cal new GregorianCalendar(TimeZone.getT
    imeZone("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

40
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)
Write a Comment
User Comments (0)
About PowerShow.com