Module: FatCore::Numeric
- Included in:
- Numeric
- Defined in:
- lib/fat_core/numeric.rb
Instance Method Summary collapse
-
#commas(places = nil) ⇒ String
Convert this number into a string and insert grouping commas into the whole number part and round the decimal part to
places
decimal places, with the default number of places being zero for an integer and 4 for a non-integer. -
#group(places = nil, delim = ',') ⇒ String
Convert this number into a string and insert grouping delimiter character,
delim
into the whole number part and round the decimal part toplaces
decimal places, with the default number of places being zero for an integer and 4 for a non-integer. -
#int_if_whole ⇒ Numeric, Integer
Return an Integer type, but only if the fractional part of self is zero; otherwise just return self.
-
#secs_to_hms ⇒ String
Convert self, regarded as a number of seconds, into a string of the form HH:MM:SS.dd, that is to hours, minutes and seconds and fractions of seconds.
-
#signum ⇒ Integer
Return the signum function for this number, i.e., 1 for a positive number, 0 for zero, and -1 for a negative number.
-
#tex_quote ⇒ Object
Quote self for use in TeX documents.
-
#whole? ⇒ Boolean
Return whether this is a whole number.
Instance Method Details
permalink #commas(places = nil) ⇒ String
Convert this number into a string and insert grouping commas into the
whole number part and round the decimal part to places
decimal places,
with the default number of places being zero for an integer and 4 for a
non-integer. The fractional part is padded with zeroes on the right to
come out to places
digits after the decimal place.
38 39 40 |
# File 'lib/fat_core/numeric.rb', line 38 def commas(places = nil) group(places, ',') end |
permalink #group(places = nil, delim = ',') ⇒ String
Convert this number into a string and insert grouping delimiter character,
delim
into the whole number part and round the decimal part to places
decimal places, with the default number of places being zero for an
integer and 4 for a non-integer. The fractional part is padded with zeroes
on the right to come out to places
digits after the decimal place. This
is the same as #commas, but allows the delimiter to be any string.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/fat_core/numeric.rb', line 59 def group(places = nil, delim = ',') # Return number as a string with embedded commas # for nice printing; round to places places after # the decimal # Only convert to string numbers with exponent unless they are # less than 1 (to ensure that really small numbers round to 0.0) return to_s if abs > 1.0 && to_s =~ /e/ # Round if places given str = if places.nil? whole? ? to_i.to_s : to_f.to_s else to_f.round(places).to_s end # Break the number into parts; underscores are possible in all components. str =~ /\A([-+])?([\d_]*)((\.)?([\d_]*))?([eE][+-]?[\d_]+)?\z/ sig = $1 || '' whole = $2 ? $2.delete('_') : '' frac = $5 || '' exp = $6 || '' # Pad out the fractional part with zeroes to the right unless places.nil? n_zeroes = [places - frac.length, 0].max frac += '0' * n_zeroes if n_zeroes.positive? end # Place the commas in the whole part only whole = whole.reverse whole.gsub!(/([0-9]{3})/, "\\1#{delim}") whole.gsub!(/#{Regexp.escape(delim)}$/, '') whole.reverse! if frac.blank? # || places <= 0 sig + whole + exp else sig + whole + '.' + frac + exp end end |
permalink #int_if_whole ⇒ Numeric, Integer
Return an Integer type, but only if the fractional part of self is zero; otherwise just return self.
120 121 122 |
# File 'lib/fat_core/numeric.rb', line 120 def int_if_whole whole? ? floor : self end |
permalink #secs_to_hms ⇒ String
Convert self, regarded as a number of seconds, into a string of the form HH:MM:SS.dd, that is to hours, minutes and seconds and fractions of seconds.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/fat_core/numeric.rb', line 131 def secs_to_hms frac = self % 1 mins, secs = divmod(60) hrs, mins = mins.divmod(60) if frac.round(5) > 0.0 '%02d:%02d:%02d.%d' % [hrs, mins, secs, frac.round(5) * 100] else '%02d:%02d:%02d' % [hrs, mins, secs] end end |
permalink #signum ⇒ Integer
Return the signum function for this number, i.e., 1 for a positive number, 0 for zero, and -1 for a negative number.
15 16 17 18 19 20 21 22 23 |
# File 'lib/fat_core/numeric.rb', line 15 def signum if positive? 1 elsif negative? -1 else 0 end end |
permalink #tex_quote ⇒ Object
Quote self for use in TeX documents. Since number components are not
special to TeX, this just applies #to_s
144 145 146 |
# File 'lib/fat_core/numeric.rb', line 144 def tex_quote to_s end |
permalink #whole? ⇒ Boolean
Return whether this is a whole number.
108 109 110 |
# File 'lib/fat_core/numeric.rb', line 108 def whole? floor == self end |