Skip to content

Instantly share code, notes, and snippets.

@kt103099
Created April 23, 2009 23:43
Show Gist options
  • Save kt103099/100844 to your computer and use it in GitHub Desktop.
Save kt103099/100844 to your computer and use it in GitHub Desktop.
# converts the number to a truncated number
# typically used for display of currency values
def smart_truncate(num)
return '--' if num.nil?
display_val = case
when num.abs > 999999: # 999,999 - round to 'X.YY M'
"%.#{2}f M" % (num / 1000000)
when num.abs > 999: # 999 - round to an integer
number_with_delimiter(number_with_precision(num, 0))
else
number_with_delimiter(number_with_precision(num, 2))
end.sub(/([0-9])\.?0+ /, '\1 ' )
return display_val
rescue
'Error'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment