Module: Perception::NumericI
- Included in:
- Numeric
- Defined in:
- lib/perception/numeric.rb
Intelligent round: slash needless digits collapse
-
#significant(precision = 3) ⇒ Numeric
Intelligent round: slash needless digits.
Easily human readable numbers collapse
-
#inspect_see(options = {}) ⇒ String
Formats a number for easily human readability.
Instance Method Details
#inspect_see(options = {}) ⇒ String
Formats a number for easily human readability. Example: (using .inspect_see without options)
input (barrier) output (barrier) note
-------------------------------------------------------------------------------------------------------
-7213541 ########-7 210 000 ######## Intelligent fallback strategy if space is not sufficient
7213541 ########7 210 000 ########
-553337 ######## -553 000 ########
553337 ######## 553 000 ######## Intelligent round: slash needless digits
-12567.89 ######## -12 600 ########
12567.89 ######## 12 600 ######## Thousands separator only if necessary
-1256.789 ######## -1260 ########
1256.789 ######## 1260 ########
-123.56789 ######## -124 ########
123.56789 ######## 124 ########
100.01 ######## 100 ########
12.0 ######## 12 ########
12 ######## 12 ########
-12.56789 ######## -12.6 ########
12.56789 ######## 12.6 ########
-1.256789 ######## -1.26 ########
1.256789 ######## 1.26 ########
-0.1256789 ######## -0.126 ########
0.1256789 ######## 0.126 ########
-0.01256789 ######## -0.0126########
0.01256789 ######## 0.0126########
-0.001256789 ######## -0.00126########
0.001256789 ######## 0.00126########
-0.0001256789 ######## -0.000126########
0.0001256789 ######## 0.000126########
Features:
-
aligned right with the separator as reference point
-
you can preced a currency symbol or append units, without destroying the alignment
-
intelligent fallback strategy if the predetermined space is not sufficient
-
thousands separator if necessary, and only if necessary
-
only the significant digits are specified so that the magnitude can be easily realised (see #significant)
If you don’t need alignment and thousands separator, use significant instead.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/perception/numeric.rb', line 138 def inspect_see(={}) precision = [:precision] || 3 pre = [:pre] || '' past = [:past] || '' space_l = [:space_l] || 9 + pre.size space_r = [:space_r] || (self.kind_of?(Integer) ? (0+past.size) : (5+past.size)) # Integer oder Float? separator = [:separator] || '.' delimiter = [:delimiter] || ' ' # auf die signifikanten Stellen reduzieren und aufteilen zusammengestrichen = self.significant(precision) zahlenteile = zusammengestrichen.to_s.split('.') # Tausendertrennzeichen und Pre zahlenteile[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") if self.abs >= 10000 zahlenteile[0] = pre + zahlenteile[0] unless pre.empty? # übereinanderstehende Formatierung result = zahlenteile[0].rjust(space_l) if zahlenteile[1] result += (separator + zahlenteile[1] + past).ljust( space_r ) #result += separator + (zahlenteile[1]).ljust( space_r-1 ) else result += past + (' ' * (space_r-past.size)) end # zu großen Platzbedarf korrigieren if result.size >= space_l+space_r+1 if (space_l - zahlenteile[0].size) < ((space_r-1) - zahlenteile[1].size) return result.strip.ljust(space_l+space_r)#.gsub(/ /,'_') # linksbündig ausgeben else return result.strip.rjust(space_l+space_r)#.gsub(/ /,'_') # rechtsbündig ausgeben end # Platz reicht aus else return result#.gsub(/ /,'_') end end |
#significant(precision = 3) ⇒ Numeric
Intelligent round: slash needless digits. Specify only the significant digits so that the magnitude can be easily realised. Needless are digits that are
-
beyond the measurement precision or
-
not perceived by humans (instead they disturb the reception)
Example:
input (+/-) result (+) result (-) result.class
----------------------------------------------------------
12567.89 12600 -12600 Fixnum
1256.789 1260 -1260 Fixnum
123.56789 124 -124 Fixnum
100.01 100 -100 Fixnum
100.0 100 -100 Fixnum
99.9 99.9 -99.9 Float
12.0 12 -12 Fixnum
12 12 -12 Fixnum
12.56789 12.6 -12.6 Float
1.256789 1.26 -1.26 Float
1.5 1.5 -1.5 Float
0 0 0 Fixnum
0.1256789 0.126 -0.126 Float
0.01256789 0.0126 -0.0126 Float
0.001256789 0.00126 -0.00126 Float
0.0001256789 0.000126 -0.000126 Float
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/perception/numeric.rb', line 49 def significant( precision = 3) # sprintf mit gemischter Schreibweise fucking_c_lib_result = sprintf("%.#{precision}g", self) fucking_c_lib_result =~ /^(.*)e/ meine_zahl = $+ || fucking_c_lib_result exponent = $' # pp meine_zahl # pp exponent # ohne Exponent if exponent.nil? result = meine_zahl.to_f if result.abs >= (10**(precision-1)) || result == result.to_i return result.to_i else return result end # mit Exponent else result = meine_zahl.to_f * 10**(exponent.to_i) if exponent[0..0] == '+' return result.to_i else return result end end # Expontent? end |