mirror of
https://github.com/jbranchaud/til
synced 2026-10-30 16:13:26 +00:00
Add Format Amount As Currency as a Rails TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||||
|
|
||||||
_1880 TILs and counting..._
|
_1881 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
|
|
||||||
@@ -1221,6 +1221,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Find Records With Multiple Associated Records](rails/find-records-with-multiple-associated-records.md)
|
- [Find Records With Multiple Associated Records](rails/find-records-with-multiple-associated-records.md)
|
||||||
- [Flipper Discards Actors When Feature Fully Enabled](rails/flipper-discards-actors-when-feature-fully-enabled.md)
|
- [Flipper Discards Actors When Feature Fully Enabled](rails/flipper-discards-actors-when-feature-fully-enabled.md)
|
||||||
- [Force All Users To Sign Out](rails/force-all-users-to-sign-out.md)
|
- [Force All Users To Sign Out](rails/force-all-users-to-sign-out.md)
|
||||||
|
- [Format Amount As Currency](rails/format-amount-as-currency.md)
|
||||||
- [Format DateTime With Builtin Formats](rails/format-datetime-with-builtin-formats.md)
|
- [Format DateTime With Builtin Formats](rails/format-datetime-with-builtin-formats.md)
|
||||||
- [Format Specific html.erb Template Files](rails/format-specific-html-erb-template-files.md)
|
- [Format Specific html.erb Template Files](rails/format-specific-html-erb-template-files.md)
|
||||||
- [Generate A Model](rails/generate-a-model.md)
|
- [Generate A Model](rails/generate-a-model.md)
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# Format Amount As Currency
|
||||||
|
|
||||||
|
I was checking various sums of dollar amounts in a Rails console recently.
|
||||||
|
However, they are returned as `BigDecimal` and display in exponential notation.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
PaymentLink.joins(:payment).during(:last_month).sum("payments.amount")
|
||||||
|
# => 0.12345678e6
|
||||||
|
```
|
||||||
|
|
||||||
|
When it comes to dollar amounts I find that notation to be hard to parse at a
|
||||||
|
glance. With Rails' help I can quickly format them as currency with the
|
||||||
|
[`number_to_currency`](https://api.rubyonrails.org/classes/ActiveSupport/NumberHelper.html#method-i-number_to_currency)
|
||||||
|
method from `ActionView`'s `NumberHelper`.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
include ActionView::Helpers::NumberHelper
|
||||||
|
number_to_currency(PaymentLink.joins(:payment).during(:last_month).sum("payments.amount"))
|
||||||
|
# => "$123,456.78"
|
||||||
|
```
|
||||||
|
|
||||||
|
There are some options that can be passed to `number_to_currency` to further
|
||||||
|
customize it, like `precision`:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
number_to_currency(PaymentLink.joins(:payment).during(:last_month).sum("payments.amount"), precision: 0)
|
||||||
|
# => "$123,457"
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user