Skip to content

Instantly share code, notes, and snippets.

@nashirox
Last active November 19, 2021 01:05
Show Gist options
  • Save nashirox/c1e6c464f65d466aa83b220521732aa5 to your computer and use it in GitHub Desktop.
Save nashirox/c1e6c464f65d466aa83b220521732aa5 to your computer and use it in GitHub Desktop.
小数点以下の桁数を取得
def digits_after_decimal_point(val)
return unless val.is_a?(Numeric)
digits = 0
while(val != val.to_i)
digits += 1
val *= 10
end
digits
end
@nashirox
Copy link
Author

sample

list = [1.0, 1.1, 1000000000.1, 1.0000000001]
#=> [1.0, 1.1, 1000000000.1, 1.0000000001]
list.map { |i| digits_after_decimal_point(i) }
#=> [0, 1, 1, 10]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment