Class: Dcc::QuantityFormat::Formatter
- Inherits:
-
Object
- Object
- Dcc::QuantityFormat::Formatter
- Defined in:
- lib/dcc/quantity_format/formatter.rb
Overview
SmartCom-style quantity pretty-printing. Significant digits derived from uncertainty (PTB convention: 2 sig digits in uncertainty).
Instance Attribute Summary collapse
-
#uncertainty ⇒ Object
readonly
Returns the value of attribute uncertainty.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(value:, uncertainty: nil, unit: nil) ⇒ Formatter
constructor
A new instance of Formatter.
-
#to_latex ⇒ String
LaTeX siunitx:
\qty{42.00 +- 0.05}{\kelvin}. -
#to_long ⇒ String
Verbose:
42.00 ± 0.05 K. -
#to_short ⇒ String
Compact notation:
42.00(5) K.
Constructor Details
#initialize(value:, uncertainty: nil, unit: nil) ⇒ Formatter
Returns a new instance of Formatter.
13 14 15 16 17 |
# File 'lib/dcc/quantity_format/formatter.rb', line 13 def initialize(value:, uncertainty: nil, unit: nil) @value = BigDecimal(value.to_s) @uncertainty = uncertainty.nil? ? nil : BigDecimal(uncertainty.to_s) @unit = unit end |
Instance Attribute Details
#uncertainty ⇒ Object (readonly)
Returns the value of attribute uncertainty.
8 9 10 |
# File 'lib/dcc/quantity_format/formatter.rb', line 8 def uncertainty @uncertainty end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
8 9 10 |
# File 'lib/dcc/quantity_format/formatter.rb', line 8 def unit @unit end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
8 9 10 |
# File 'lib/dcc/quantity_format/formatter.rb', line 8 def value @value end |
Instance Method Details
#to_latex ⇒ String
Returns LaTeX siunitx: \qty{42.00 +- 0.05}{\kelvin}.
37 38 39 40 41 42 43 |
# File 'lib/dcc/quantity_format/formatter.rb', line 37 def to_latex if uncertainty.nil? "\\qty{#{format_value}}{#{unit || ''}}" else "\\qty{#{format_value} +- #{format_uncertainty}}{#{unit || ''}}" end end |
#to_long ⇒ String
Returns verbose: 42.00 ± 0.05 K.
28 29 30 31 32 33 34 |
# File 'lib/dcc/quantity_format/formatter.rb', line 28 def to_long if uncertainty.nil? "#{format_value}#{unit_suffix}" else "#{format_value} ± #{format_uncertainty}#{unit_suffix}" end end |
#to_short ⇒ String
Returns compact notation: 42.00(5) K.
20 21 22 23 24 25 |
# File 'lib/dcc/quantity_format/formatter.rb', line 20 def to_short return format_value.to_s + unit_suffix if uncertainty.nil? v_str, u_str = align_value_and_uncertainty "#{v_str}(#{u_str})#{unit_suffix}" end |