module TwitterCldr::Formatters
Public Instance Methods
# File lib/twitter_cldr/formatters/numbers/rbnf/rule_set.rb, line 199 def contains_fraction?(number) number != number.floor end
# File lib/twitter_cldr/formatters/numbers/rbnf/rule_set.rb, line 253 def is_numeric?(val) !!(val.to_s =~ /\A[\d]+\.?[\d]{0,}\z/) end
If the rule set is a regular rule set, do the following:
If the rule set includes a master rule (and the number was passed in as a double), use the master rule. (If the number being formatted was passed in as a long, the master rule is ignored.) If the number is negative, use the negative-number rule. If the number has a fractional part and is greater than 1, use the improper fraction rule. If the number has a fractional part and is between 0 and 1, use the proper fraction rule. Binary-search the rule list for the rule with the highest base value less than or equal to the number. If that rule has two substitutions, its base value is not an even multiple of its divisor, and the number is an even multiple of the rule's divisor, use the rule that precedes it in the rule list. Otherwise, use the rule itself.
# File lib/twitter_cldr/formatters/numbers/rbnf/rule_set.rb, line 173 def normal_rule_for(number) if rule = master_rule rule elsif number < 0 && rule = negative_rule rule elsif contains_fraction?(number) && number > 1 && rule = improper_fraction_rule rule elsif contains_fraction?(number) && number > 0 && number < 1 && rule = proper_fraction_rule rule else if rule = rule_for_value(number.abs) use_prev_rule = rule.substitution_count == 2 && !rule.even_multiple_of?(rule.base_value) && rule.even_multiple_of?(number) if use_prev_rule previous_rule_for(rule) else rule end else rules[search_start_index] || rules.first end end end
# File lib/twitter_cldr/formatters/numbers/rbnf/rule_set.rb, line 203 def rule_index_for(base_value) if rule_index = special_rule_index_for(base_value) return rule_index end if is_numeric?(base_value) # binary search (base_value must be a number for this to work) low = search_start_index high = rules.size - 1 while low <= high mid = (low + high) / 2 mid_base_value = rules[mid].base_value case when mid_base_value > base_value high = mid - 1 when mid_base_value < base_value low = mid + 1 else break end end # Binary-search the rule list for the rule with the highest base value less than or equal to the number. if rules[mid].base_value <= base_value mid else mid > 0 ? mid - 1 : mid end end end
# File lib/twitter_cldr/formatters/numbers/rbnf/rule_set.rb, line 245 def search_start_index @search_start_index ||= begin rules.find_index do |rule| is_numeric?(rule.base_value) end || 0 end end
# File lib/twitter_cldr/formatters/numbers/rbnf/rule_set.rb, line 236 def special_rule_index_for(base_value) (0...search_start_index).each do |i| if rules[i].base_value == base_value return i end end nil end