class RuboCop::Cop::Style::ExponentialNotation
12e5
1e4
32e6
# good
120e4
0.1e5
3.2e7
# bad
# trailing zeroes.
# Enforces the mantissa to have no decimal part and no
@example EnforcedStyle: integral
1.232e9
1.2e6
10e3
32e6
# good
1232e6
12e5
0.1e5
3.2e7
# bad
# mantissa should be between 0.1 (inclusive) and 1000 (exclusive)
# Enforces using multiple of 3 exponents,
@example EnforcedStyle: engineering
3.14
1.17e6
3e3
1e7
# good
3.14e0
11.7e5
0.3e4
10e6
# bad
# Enforces a mantissa between 1 (inclusive) and 10 (exclusive).
@example EnforcedStyle: scientific (default)
trailing zeroes.
* ‘integral` which enforces the mantissa to always be a whole number without
to be between 0.1 (inclusive) and 10 (exclusive).
* `engineering` which enforces the exponent to be a multiple of 3 and the mantissa
* `scientific` which enforces a mantissa between 1 (inclusive) and 10 (exclusive).
for numbers in the code (eg 1.2e4). Different styles are supported:
Enforces consistency when using exponential notation
def engineering?(node)
def engineering?(node) mantissa, exponent = node.source.split('e') return false unless /^-?\d+$/.match?(exponent) return false unless (exponent.to_i % 3).zero? return false if /^-?\d{4}/.match?(mantissa) return false if /^-?0\d/.match?(mantissa) return false if /^-?0.0/.match?(mantissa) true end
def integral(node)
def integral(node) mantissa, = node.source.split('e') /^-?[1-9](\d*[1-9])?$/.match?(mantissa) end
def message(_node)
def message(_node) MESSAGES[style] end
def offense?(node)
def offense?(node) return false unless node.source['e'] case style when :scientific !scientific?(node) when :engineering !engineering?(node) when :integral !integral(node) else false end end
def on_float(node)
def on_float(node) add_offense(node) if offense?(node) end
def scientific?(node)
def scientific?(node) mantissa, = node.source.split('e') /^-?[1-9](\.\d*[0-9])?$/.match?(mantissa) end