Skip to content

Instantly share code, notes, and snippets.

@thescubageek
Last active March 7, 2018 04:07
Show Gist options
  • Save thescubageek/85236625fbf98cf0864d05a659ded9f4 to your computer and use it in GitHub Desktop.
Save thescubageek/85236625fbf98cf0864d05a659ded9f4 to your computer and use it in GitHub Desktop.
steem_percentage.rb
module SteemPercentage
extend ActiveSupport::Concern
STEEM_MAX_PERCENT = 10000.0
# converts to [0.0-1.0] => percent_decimal(8000) == 0.8
def percent_decimal(num)
num.to_f / STEEM_MAX_PERCENT
end
# converts to [0.0-100.0] => percent(8000) == 80.0
def percent(num)
percent_decimal(num) * 100.0
end
# converts to % format => percent_str(8000) == '80.0%'
def percent_str(num, precision=1)
"#{number_with_precision(percent(num), precision)}%"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment