25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/vv/numeric_methods.rb', line 25
def readable_number precision: 3, significant: false
message = "Cannot make imaginary number readable"
fail message if imaginary?
_int_digits,
_remaining_digits = self.real_digits.split String.period
_int_digits = \
_int_digits.after String.dash, safe: false
if _int_digits.size > 4
offset = ( _int_digits.size ) % 3
required = ( _int_digits.size - 1 ) / 3
index = required * 3 + ( offset % -3 )
while index > 0
_int_digits.insert(index, String.comma)
index -= 3
end
end
response = \
real.negative? ? String.dash : String.empty_string
response += _int_digits
return response unless _remaining_digits
response += String.period
last_digit = significant ? -1 : ( precision )
if significant || precision >= _remaining_digits.size
dec_portion = _remaining_digits
else
rounded = self.real.to_f.round(precision)
dec_portion = rounded.to_s.split(".")[-1][0..last_digit]
end
response + dec_portion
end
|