Module: Measurand::Format

Defined in:
lib/Measurand/Format.rb

Class Method Summary collapse

Class Method Details

.decimals(place) ⇒ Object



39
40
41
# File 'lib/Measurand/Format.rb', line 39

def decimals(place)
  place < 0 ? -place : 0
end

.round_to_place(number, place) ⇒ Object



34
35
36
37
# File 'lib/Measurand/Format.rb', line 34

def round_to_place(number, place)
  factor = 10.0 ** place
  (number / factor).round * factor
end

.round_uncertainty(uncertainty) ⇒ Object

Returns [rounded_uncertainty, place], where place is the power of ten of the least significant shown digit (negative for digits after the point).



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/Measurand/Format.rb', line 19

def round_uncertainty(uncertainty)
  exponent = ::Math.log10(uncertainty).floor
  leading = (uncertainty / 10.0 ** (exponent - 2)).round
  if leading < 355
    significant_figures = 2
  elsif leading < 950
    significant_figures = 1
  else
    significant_figures = 2
    exponent += 1
  end
  place = exponent - significant_figures + 1
  [round_to_place(uncertainty, place), place]
end