Class: javajava::lang::Number
- Defined in:
- lib/jactive_support/java_ext/number.rb
Constant Summary collapse
- NUMBER_FORMATS =
{ :number => lambda {|value, locale| number_format_instance(:number, locale).format(value.to_primitive)}, :currency => lambda {|value, locale| number_format_instance(:currency, locale).format(value.to_primitive)}, :percent => lambda {|value, locale| number_format_instance(:percent, locale).format(value.to_primitive)}, :scientific => lambda {|value, locale| number_format_instance(:scientific, locale).format(value.to_primitive)}, :integer => lambda {|value, locale| number_format_instance(:integer, locale).format(value.to_primitive)}, :default => lambda {|value, locale| number_format_instance(:default, locale).format(value.to_primitive)}, }
Class Method Summary collapse
Instance Method Summary collapse
- #format_number(format, locale = nil) ⇒ Object
- #to_formatted_s(format = :default, locale = nil) ⇒ Object (also: #to_s)
- #to_primitive ⇒ Object
Class Method Details
.number_format_instance(format = :default, locale = nil) ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/jactive_support/java_ext/number.rb', line 2 def self.number_format_instance(format=:default, locale=nil) case format when :number then ::Java::JavaText::NumberFormat.getNumberInstance(locale.to_locale) when :currency then ::Java::JavaText::NumberFormat.getCurrencyInstance(locale.to_locale) when :percent then ::Java::JavaText::NumberFormat.getPercentInstance(locale.to_locale) when :scientific then ::Java::JavaText::NumberFormat.getScientificInstance(locale.to_locale) when :integer then ::Java::JavaText::NumberFormat.getIntegerInstance(locale.to_locale) when :default then ::Java::JavaText::NumberFormat.getInstance(locale.to_locale) end end |
Instance Method Details
#format_number(format, locale = nil) ⇒ Object
29 30 31 32 33 |
# File 'lib/jactive_support/java_ext/number.rb', line 29 def format_number(format, locale=nil) symbols = ::Java::JavaText::DecimalFormatSymbols.new(locale.to_locale) formatter = ::Java::JavaText::DecimalFormat.new(format, symbols) formatter.format(self) end |
#to_formatted_s(format = :default, locale = nil) ⇒ Object Also known as: to_s
22 23 24 25 |
# File 'lib/jactive_support/java_ext/number.rb', line 22 def to_formatted_s(format=:default, locale=nil) return to_default_s unless formatter = self.class::NUMBER_FORMATS[format] formatter.respond_to?(:call) ? (formatter.arity==2 ? formatter.call(self, locale) : formatter.call(self)).to_s : format_number(formatter) end |
#to_primitive ⇒ Object
35 36 37 |
# File 'lib/jactive_support/java_ext/number.rb', line 35 def to_primitive self end |