Return this exception’s class name and message.
Round to the nearest integer (by default), returning the result as a BigDecimal
if n is specified, or as an Integer
if it isn’t.
BigDecimal('3.14159').round #=> 3 BigDecimal('8.7').round #=> 9 BigDecimal('-9.9').round #=> -10 BigDecimal('3.14159').round(2).class.name #=> "BigDecimal" BigDecimal('3.14159').round.class.name #=> "Integer"
If n is specified and positive, the fractional part of the result has no more than that many digits.
If n is specified and negative, at least that many digits to the left of the decimal point will be 0 in the result.
BigDecimal('3.14159').round(3) #=> 3.142 BigDecimal('13345.234').round(-2) #=> 13300.0
The value of the optional mode argument can be used to determine how rounding is performed; see BigDecimal.mode
.
Returns self if the value is non-zero, nil otherwise.
Returns a string representation of self.
BigDecimal("1234.5678").inspect #=> "0.12345678e4"
Returns the exponent of the BigDecimal
number, as an Integer
.
If the number can be represented as 0.xxxxxx*10**n where xxxxxx is a string of digits with no leading zeros, then n is the exponent.
Returns true
if rat
is greater than 0.
Returns true
if rat
is less than 0.
Returns rat
rounded to the nearest value with a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns a rational when ndigits
is positive, otherwise returns an integer.
Rational(3).round #=> 3 Rational(2, 3).round #=> 1 Rational(-3, 2).round #=> -2 # decimal - 1 2 3 . 4 5 6 # ^ ^ ^ ^ ^ ^ # precision -3 -2 -1 0 +1 +2 Rational('-123.456').round(+1).to_f #=> -123.5 Rational('-123.456').round(-1) #=> -120
The optional half
keyword argument is available similar to Float#round
.
Rational(25, 100).round(1, half: :up) #=> (3/10) Rational(25, 100).round(1, half: :down) #=> (1/5) Rational(25, 100).round(1, half: :even) #=> (1/5) Rational(35, 100).round(1, half: :up) #=> (2/5) Rational(35, 100).round(1, half: :down) #=> (3/10) Rational(35, 100).round(1, half: :even) #=> (2/5) Rational(-25, 100).round(1, half: :up) #=> (-3/10) Rational(-25, 100).round(1, half: :down) #=> (-1/5) Rational(-25, 100).round(1, half: :even) #=> (-1/5)
Returns the value as a string for inspection.
Rational(2).inspect #=> "(2/1)" Rational(-8, 6).inspect #=> "(-4/3)" Rational('1/2').inspect #=> "(1/2)"
Parses the given representation of date and time with the given template, and returns a hash of parsed elements. _strptime does not support specification of flags and width unlike strftime.
Date._strptime('2001-02-03', '%Y-%m-%d') #=> {:year=>2001, :mon=>2, :mday=>3}
See also strptime(3) and strftime
.
Parses the given representation of date and time with the given template, and creates a date object. strptime does not support specification of flags and width unlike strftime.
Date.strptime('2001-02-03', '%Y-%m-%d') #=> #<Date: 2001-02-03 ...> Date.strptime('03-02-2001', '%d-%m-%Y') #=> #<Date: 2001-02-03 ...> Date.strptime('2001-034', '%Y-%j') #=> #<Date: 2001-02-03 ...> Date.strptime('2001-W05-6', '%G-W%V-%u') #=> #<Date: 2001-02-03 ...> Date.strptime('2001 04 6', '%Y %U %w') #=> #<Date: 2001-02-03 ...> Date.strptime('2001 05 6', '%Y %W %u') #=> #<Date: 2001-02-03 ...> Date.strptime('sat3feb01', '%a%d%b%y') #=> #<Date: 2001-02-03 ...>
See also strptime(3) and strftime
.
Returns true if the date is Sunday.
Returns the value as a string for inspection.
Date.new(2001,2,3).inspect #=> "#<Date: 2001-02-03>" DateTime.new(2001,2,3,4,5,6,'-7').inspect #=> "#<DateTime: 2001-02-03T04:05:06-07:00>"
Formats date according to the directives in the given format string. The directives begin with a percent (%) character. Any text not listed as a directive will be passed through to the output string.
A directive consists of a percent (%) character, zero or more flags, an optional minimum field width, an optional modifier, and a conversion specifier as follows.
%<flags><width><modifier><conversion>
Flags:
- don't pad a numerical output. _ use spaces for padding. 0 use zeros for padding. ^ upcase the result string. # change case.
The minimum field width specifies the minimum width.
The modifiers are “E”, “O”, “:”, “::” and “:::”. “E” and “O” are ignored. No effect to result currently.
Format directives:
Date (Year, Month, Day): %Y - Year with century (can be negative, 4 digits at least) -0001, 0000, 1995, 2009, 14292, etc. %C - year / 100 (round down. 20 in 2009) %y - year % 100 (00..99) %m - Month of the year, zero-padded (01..12) %_m blank-padded ( 1..12) %-m no-padded (1..12) %B - The full month name (``January'') %^B uppercased (``JANUARY'') %b - The abbreviated month name (``Jan'') %^b uppercased (``JAN'') %h - Equivalent to %b %d - Day of the month, zero-padded (01..31) %-d no-padded (1..31) %e - Day of the month, blank-padded ( 1..31) %j - Day of the year (001..366) Time (Hour, Minute, Second, Subsecond): %H - Hour of the day, 24-hour clock, zero-padded (00..23) %k - Hour of the day, 24-hour clock, blank-padded ( 0..23) %I - Hour of the day, 12-hour clock, zero-padded (01..12) %l - Hour of the day, 12-hour clock, blank-padded ( 1..12) %P - Meridian indicator, lowercase (``am'' or ``pm'') %p - Meridian indicator, uppercase (``AM'' or ``PM'') %M - Minute of the hour (00..59) %S - Second of the minute (00..60) %L - Millisecond of the second (000..999) %N - Fractional seconds digits, default is 9 digits (nanosecond) %3N millisecond (3 digits) %15N femtosecond (15 digits) %6N microsecond (6 digits) %18N attosecond (18 digits) %9N nanosecond (9 digits) %21N zeptosecond (21 digits) %12N picosecond (12 digits) %24N yoctosecond (24 digits) Time zone: %z - Time zone as hour and minute offset from UTC (e.g. +0900) %:z - hour and minute offset from UTC with a colon (e.g. +09:00) %::z - hour, minute and second offset from UTC (e.g. +09:00:00) %:::z - hour, minute and second offset from UTC (e.g. +09, +09:30, +09:30:30) %Z - Equivalent to %:z (e.g. +09:00) Weekday: %A - The full weekday name (``Sunday'') %^A uppercased (``SUNDAY'') %a - The abbreviated name (``Sun'') %^a uppercased (``SUN'') %u - Day of the week (Monday is 1, 1..7) %w - Day of the week (Sunday is 0, 0..6) ISO 8601 week-based year and week number: The week 1 of YYYY starts with a Monday and includes YYYY-01-04. The days in the year before the first week are in the last week of the previous year. %G - The week-based year %g - The last 2 digits of the week-based year (00..99) %V - Week number of the week-based year (01..53) Week number: The week 1 of YYYY starts with a Sunday or Monday (according to %U or %W). The days in the year before the first week are in week 0. %U - Week number of the year. The week starts with Sunday. (00..53) %W - Week number of the year. The week starts with Monday. (00..53) Seconds since the Unix Epoch: %s - Number of seconds since 1970-01-01 00:00:00 UTC. %Q - Number of milliseconds since 1970-01-01 00:00:00 UTC. Literal string: %n - Newline character (\n) %t - Tab character (\t) %% - Literal ``%'' character Combination: %c - date and time (%a %b %e %T %Y) %D - Date (%m/%d/%y) %F - The ISO 8601 date format (%Y-%m-%d) %v - VMS date (%e-%b-%Y) %x - Same as %D %X - Same as %T %r - 12-hour time (%I:%M:%S %p) %R - 24-hour time (%H:%M) %T - 24-hour time (%H:%M:%S) %+ - date(1) (%a %b %e %H:%M:%S %Z %Y)
This method is similar to the strftime() function defined in ISO C and POSIX. Several directives (%a, %A, %b, %B, %c, %p, %r, %x, %X, %E*, %O* and %Z) are locale dependent in the function. However, this method is locale independent. So, the result may differ even if the same format string is used in other systems such as C. It is good practice to avoid %x and %X because there are corresponding locale independent representations, %D and %T.
Examples:
d = DateTime.new(2007,11,19,8,37,48,"-06:00") #=> #<DateTime: 2007-11-19T08:37:48-0600 ...> d.strftime("Printed on %m/%d/%Y") #=> "Printed on 11/19/2007" d.strftime("at %I:%M%p") #=> "at 08:37AM"
Various ISO 8601 formats:
%Y%m%d => 20071119 Calendar date (basic) %F => 2007-11-19 Calendar date (extended) %Y-%m => 2007-11 Calendar date, reduced accuracy, specific month %Y => 2007 Calendar date, reduced accuracy, specific year %C => 20 Calendar date, reduced accuracy, specific century %Y%j => 2007323 Ordinal date (basic) %Y-%j => 2007-323 Ordinal date (extended) %GW%V%u => 2007W471 Week date (basic) %G-W%V-%u => 2007-W47-1 Week date (extended) %GW%V => 2007W47 Week date, reduced accuracy, specific week (basic) %G-W%V => 2007-W47 Week date, reduced accuracy, specific week (extended) %H%M%S => 083748 Local time (basic) %T => 08:37:48 Local time (extended) %H%M => 0837 Local time, reduced accuracy, specific minute (basic) %H:%M => 08:37 Local time, reduced accuracy, specific minute (extended) %H => 08 Local time, reduced accuracy, specific hour %H%M%S,%L => 083748,000 Local time with decimal fraction, comma as decimal sign (basic) %T,%L => 08:37:48,000 Local time with decimal fraction, comma as decimal sign (extended) %H%M%S.%L => 083748.000 Local time with decimal fraction, full stop as decimal sign (basic) %T.%L => 08:37:48.000 Local time with decimal fraction, full stop as decimal sign (extended) %H%M%S%z => 083748-0600 Local time and the difference from UTC (basic) %T%:z => 08:37:48-06:00 Local time and the difference from UTC (extended) %Y%m%dT%H%M%S%z => 20071119T083748-0600 Date and time of day for calendar date (basic) %FT%T%:z => 2007-11-19T08:37:48-06:00 Date and time of day for calendar date (extended) %Y%jT%H%M%S%z => 2007323T083748-0600 Date and time of day for ordinal date (basic) %Y-%jT%T%:z => 2007-323T08:37:48-06:00 Date and time of day for ordinal date (extended) %GW%V%uT%H%M%S%z => 2007W471T083748-0600 Date and time of day for week date (basic) %G-W%V-%uT%T%:z => 2007-W47-1T08:37:48-06:00 Date and time of day for week date (extended) %Y%m%dT%H%M => 20071119T0837 Calendar date and local time (basic) %FT%R => 2007-11-19T08:37 Calendar date and local time (extended) %Y%jT%H%MZ => 2007323T0837Z Ordinal date and UTC of day (basic) %Y-%jT%RZ => 2007-323T08:37Z Ordinal date and UTC of day (extended) %GW%V%uT%H%M%z => 2007W471T0837-0600 Week date and local time and difference from UTC (basic) %G-W%V-%uT%R%:z => 2007-W47-1T08:37-06:00 Week date and local time and difference from UTC (extended)
See also strftime(3) and ::strptime
.
Parses the given representation of date and time with the given template, and returns a hash of parsed elements. _strptime does not support specification of flags and width unlike strftime.
See also strptime(3) and strftime
.
Parses the given representation of date and time with the given template, and creates a DateTime
object. strptime does not support specification of flags and width unlike strftime.
DateTime.strptime('2001-02-03T04:05:06+07:00', '%Y-%m-%dT%H:%M:%S%z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('03-02-2001 04:05:06 PM', '%d-%m-%Y %I:%M:%S %p') #=> #<DateTime: 2001-02-03T16:05:06+00:00 ...> DateTime.strptime('2001-W05-6T04:05:06+07:00', '%G-W%V-%uT%H:%M:%S%z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('2001 04 6 04 05 06 +7', '%Y %U %w %H %M %S %z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('2001 05 6 04 05 06 +7', '%Y %W %u %H %M %S %z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('-1', '%s') #=> #<DateTime: 1969-12-31T23:59:59+00:00 ...> DateTime.strptime('-1000', '%Q') #=> #<DateTime: 1969-12-31T23:59:59+00:00 ...> DateTime.strptime('sat3feb014pm+7', '%a%d%b%y%H%p%z') #=> #<DateTime: 2001-02-03T16:00:00+07:00 ...>
See also strptime(3) and strftime
.
Formats date according to the directives in the given format string. The directives begin with a percent (%) character. Any text not listed as a directive will be passed through to the output string.
A directive consists of a percent (%) character, zero or more flags, an optional minimum field width, an optional modifier, and a conversion specifier as follows.
%<flags><width><modifier><conversion>
Flags:
- don't pad a numerical output. _ use spaces for padding. 0 use zeros for padding. ^ upcase the result string. # change case. : use colons for %z.
The minimum field width specifies the minimum width.
The modifiers are “E” and “O”. They are ignored.
Format directives:
Date (Year, Month, Day): %Y - Year with century (can be negative, 4 digits at least) -0001, 0000, 1995, 2009, 14292, etc. %C - year / 100 (round down. 20 in 2009) %y - year % 100 (00..99) %m - Month of the year, zero-padded (01..12) %_m blank-padded ( 1..12) %-m no-padded (1..12) %B - The full month name (``January'') %^B uppercased (``JANUARY'') %b - The abbreviated month name (``Jan'') %^b uppercased (``JAN'') %h - Equivalent to %b %d - Day of the month, zero-padded (01..31) %-d no-padded (1..31) %e - Day of the month, blank-padded ( 1..31) %j - Day of the year (001..366) Time (Hour, Minute, Second, Subsecond): %H - Hour of the day, 24-hour clock, zero-padded (00..23) %k - Hour of the day, 24-hour clock, blank-padded ( 0..23) %I - Hour of the day, 12-hour clock, zero-padded (01..12) %l - Hour of the day, 12-hour clock, blank-padded ( 1..12) %P - Meridian indicator, lowercase (``am'' or ``pm'') %p - Meridian indicator, uppercase (``AM'' or ``PM'') %M - Minute of the hour (00..59) %S - Second of the minute (00..60) %L - Millisecond of the second (000..999) %N - Fractional seconds digits, default is 9 digits (nanosecond) %3N millisecond (3 digits) %15N femtosecond (15 digits) %6N microsecond (6 digits) %18N attosecond (18 digits) %9N nanosecond (9 digits) %21N zeptosecond (21 digits) %12N picosecond (12 digits) %24N yoctosecond (24 digits) Time zone: %z - Time zone as hour and minute offset from UTC (e.g. +0900) %:z - hour and minute offset from UTC with a colon (e.g. +09:00) %::z - hour, minute and second offset from UTC (e.g. +09:00:00) %:::z - hour, minute and second offset from UTC (e.g. +09, +09:30, +09:30:30) %Z - Equivalent to %:z (e.g. +09:00) Weekday: %A - The full weekday name (``Sunday'') %^A uppercased (``SUNDAY'') %a - The abbreviated name (``Sun'') %^a uppercased (``SUN'') %u - Day of the week (Monday is 1, 1..7) %w - Day of the week (Sunday is 0, 0..6) ISO 8601 week-based year and week number: The week 1 of YYYY starts with a Monday and includes YYYY-01-04. The days in the year before the first week are in the last week of the previous year. %G - The week-based year %g - The last 2 digits of the week-based year (00..99) %V - Week number of the week-based year (01..53) Week number: The week 1 of YYYY starts with a Sunday or Monday (according to %U or %W). The days in the year before the first week are in week 0. %U - Week number of the year. The week starts with Sunday. (00..53) %W - Week number of the year. The week starts with Monday. (00..53) Seconds since the Unix Epoch: %s - Number of seconds since 1970-01-01 00:00:00 UTC. %Q - Number of milliseconds since 1970-01-01 00:00:00 UTC. Literal string: %n - Newline character (\n) %t - Tab character (\t) %% - Literal ``%'' character Combination: %c - date and time (%a %b %e %T %Y) %D - Date (%m/%d/%y) %F - The ISO 8601 date format (%Y-%m-%d) %v - VMS date (%e-%b-%Y) %x - Same as %D %X - Same as %T %r - 12-hour time (%I:%M:%S %p) %R - 24-hour time (%H:%M) %T - 24-hour time (%H:%M:%S) %+ - date(1) (%a %b %e %H:%M:%S %Z %Y)
This method is similar to the strftime() function defined in ISO C and POSIX. Several directives (%a, %A, %b, %B, %c, %p, %r, %x, %X, %E*, %O* and %Z) are locale dependent in the function. However, this method is locale independent. So, the result may differ even if the same format string is used in other systems such as C. It is good practice to avoid %x and %X because there are corresponding locale independent representations, %D and %T.
Examples:
d = DateTime.new(2007,11,19,8,37,48,"-06:00") #=> #<DateTime: 2007-11-19T08:37:48-0600 ...> d.strftime("Printed on %m/%d/%Y") #=> "Printed on 11/19/2007" d.strftime("at %I:%M%p") #=> "at 08:37AM"
Various ISO 8601 formats:
%Y%m%d => 20071119 Calendar date (basic) %F => 2007-11-19 Calendar date (extended) %Y-%m => 2007-11 Calendar date, reduced accuracy, specific month %Y => 2007 Calendar date, reduced accuracy, specific year %C => 20 Calendar date, reduced accuracy, specific century %Y%j => 2007323 Ordinal date (basic) %Y-%j => 2007-323 Ordinal date (extended) %GW%V%u => 2007W471 Week date (basic) %G-W%V-%u => 2007-W47-1 Week date (extended) %GW%V => 2007W47 Week date, reduced accuracy, specific week (basic) %G-W%V => 2007-W47 Week date, reduced accuracy, specific week (extended) %H%M%S => 083748 Local time (basic) %T => 08:37:48 Local time (extended) %H%M => 0837 Local time, reduced accuracy, specific minute (basic) %H:%M => 08:37 Local time, reduced accuracy, specific minute (extended) %H => 08 Local time, reduced accuracy, specific hour %H%M%S,%L => 083748,000 Local time with decimal fraction, comma as decimal sign (basic) %T,%L => 08:37:48,000 Local time with decimal fraction, comma as decimal sign (extended) %H%M%S.%L => 083748.000 Local time with decimal fraction, full stop as decimal sign (basic) %T.%L => 08:37:48.000 Local time with decimal fraction, full stop as decimal sign (extended) %H%M%S%z => 083748-0600 Local time and the difference from UTC (basic) %T%:z => 08:37:48-06:00 Local time and the difference from UTC (extended) %Y%m%dT%H%M%S%z => 20071119T083748-0600 Date and time of day for calendar date (basic) %FT%T%:z => 2007-11-19T08:37:48-06:00 Date and time of day for calendar date (extended) %Y%jT%H%M%S%z => 2007323T083748-0600 Date and time of day for ordinal date (basic) %Y-%jT%T%:z => 2007-323T08:37:48-06:00 Date and time of day for ordinal date (extended) %GW%V%uT%H%M%S%z => 2007W471T083748-0600 Date and time of day for week date (basic) %G-W%V-%uT%T%:z => 2007-W47-1T08:37:48-06:00 Date and time of day for week date (extended) %Y%m%dT%H%M => 20071119T0837 Calendar date and local time (basic) %FT%R => 2007-11-19T08:37 Calendar date and local time (extended) %Y%jT%H%MZ => 2007323T0837Z Ordinal date and UTC of day (basic) %Y-%jT%RZ => 2007-323T08:37Z Ordinal date and UTC of day (extended) %GW%V%uT%H%M%z => 2007W471T0837-0600 Week date and local time and difference from UTC (basic) %G-W%V-%uT%R%:z => 2007-W47-1T08:37-06:00 Week date and local time and difference from UTC (extended)
See also strftime(3) and ::strptime
.
Works similar to parse
except that instead of using a heuristic to detect the format of the input string, you provide a second argument that describes the format of the string.
If a block is given, the year described in date
is converted by the block. For example:
Time.strptime(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
Below is a list of the formatting options:
The abbreviated weekday name (“Sun”)
The full weekday name (“Sunday”)
The abbreviated month name (“Jan”)
The full month name (“January”)
The preferred local date and time representation
Century (20 in 2009)
Day of the month (01..31)
Date
(%m/%d/%y)
Day of the month, blank-padded ( 1..31)
Equivalent to %Y-%m-%d (the ISO 8601 date format)
The last two digits of the commercial year
The week-based year according to ISO-8601 (week 1 starts on Monday and includes January 4)
Equivalent to %b
Hour of the day, 24-hour clock (00..23)
Hour of the day, 12-hour clock (01..12)
Day of the year (001..366)
hour, 24-hour clock, blank-padded ( 0..23)
hour, 12-hour clock, blank-padded ( 0..12)
Millisecond of the second (000..999)
Month of the year (01..12)
Minute of the hour (00..59)
Newline (n)
Fractional seconds digits
Meridian indicator (“AM” or “PM”)
Meridian indicator (“am” or “pm”)
time, 12-hour (same as %I:%M:%S %p)
time, 24-hour (%H:%M)
Number of seconds since 1970-01-01 00:00:00 UTC.
Second of the minute (00..60)
Tab character (t)
time, 24-hour (%H:%M:%S)
Day of the week as a decimal, Monday being 1. (1..7)
Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
VMS date (%e-%b-%Y)
Week number of year according to ISO 8601 (01..53)
Week number of the current year, starting with the first Monday as the first day of the first week (00..53)
Day of the week (Sunday is 0, 0..6)
Preferred representation for the date alone, no time
Preferred representation for the time alone, no date
Year without a century (00..99)
Year which may include century, if provided
Time
zone as hour offset from UTC (e.g. +0900)
Time
zone name
Literal “%” character
date(1) (%a %b %e %H:%M:%S %Z %Y)
require 'time' Time.strptime("2000-10-31", "%Y-%m-%d") #=> 2000-10-31 00:00:00 -0500
You must require ‘time’ to use this method.
Same as Time::gm
, but interprets the values in the local time zone.
Time.local(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 -0600
Converts time to local time (using the local time zone in effect at the creation time of time) modifying the receiver.
If utc_offset
is given, it is used instead of the local time.
t = Time.utc(2000, "jan", 1, 20, 15, 1) #=> 2000-01-01 20:15:01 UTC t.utc? #=> true t.localtime #=> 2000-01-01 14:15:01 -0600 t.utc? #=> false t.localtime("+09:00") #=> 2000-01-02 05:15:01 +0900 t.utc? #=> false
If utc_offset
is not given and time is local time, just returns the receiver.
Converts time to UTC (GMT), modifying the receiver.
t = Time.now #=> 2007-11-19 08:18:31 -0600 t.gmt? #=> false t.gmtime #=> 2007-11-19 14:18:31 UTC t.gmt? #=> true t = Time.now #=> 2007-11-19 08:18:51 -0600 t.utc? #=> false t.utc #=> 2007-11-19 14:18:51 UTC t.utc? #=> true