Skip to content

Instantly share code, notes, and snippets.

@Ji-Yuhang
Created September 21, 2017 02:24
Show Gist options
  • Save Ji-Yuhang/210253c7cc7033be28984ee5483bbba4 to your computer and use it in GitHub Desktop.
Save Ji-Yuhang/210253c7cc7033be28984ee5483bbba4 to your computer and use it in GitHub Desktop.
grape custom format
Grape::Entity.format_with :utc do |date|
date.strftime('%Y年%m月%d日 %H:%M') if date
end
Grape::Entity.format_with :date do |date|
date.strftime('%Y年%m月%d日') if date
end
Grape::Entity.format_with :datetime do |date|
date.strftime('%Y年%m月%d日 %H:%M:%S') if date
end
Grape::Entity.format_with :rmb do |money, options|
ActiveSupport::NumberHelper.number_to_currency(money, unit: '元') if money
end
Grape::Entity.format_with :rmb_p0 do |money, options|
ActiveSupport::NumberHelper.number_to_currency(money, unit: '元', precision: 0) if money
end
Grape::Entity.format_with :round do |money|
ActiveSupport::NumberHelper.number_to_rounded(money, precision: 2).to_f if money
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment