0

I have this method below:

  def formatted_from_net_per_day
    return unless object.price.available?
    formatted_price_for(object.price.from_price)
  end

As an example, it outputs:

#<Money fractional:78571 currency:GBP>

The formatted_price_for method is as below where it strips away the cents.

  def formatted_price_for(price)
    formatted_price = h.money_without_cents_and_with_symbol(price)
    unit = I18n.t("space.pricings.pricing.day")

    format('%s/%s', formatted_price, unit)
  end

Instead of stripping the cents away, I want to round up the money and retain the currency. So using the example output above - £785.71 -> £786

How can I achieve that?

ejderuby
  • 710
  • 5
  • 21
drifterOcean19
  • 372
  • 5
  • 24
  • Just [reading the README](https://github.com/RubyMoney/money-rails#configuration-parameters) and checking [the source code](https://github.com/RubyMoney/money-rails/blob/c8c1dc8501f6c33e3f104289e722570d8b747bde/lib/money-rails/helpers/action_view_extension.rb#L33-L50)... Does this work? `h.money_without_cents(price, symbol: true, rounding_mode: BigDecimal::ROUND_CEILING)` – Tom Lord Jul 16 '19 at 12:56
  • That didn't work - it gave me £785 – drifterOcean19 Jul 16 '19 at 13:02
  • Also tried - `h.humanized_money(space.price.from_price, symbol: true, rounding_mode: BigDecimal::ROUND_CEILING)` but gave me £785.71 – drifterOcean19 Jul 16 '19 at 13:07
  • This is ugly, but what about `Money.with_rounding_mode(BigDecimal::ROUND_CEILING) { h.money_with_cents_and_with_symbol(price) }`? ([link](https://github.com/RubyMoney/money/blob/8754d9ae614b8903525746589a6fed104d469344/lib/money/money.rb#L234-L239)) – Tom Lord Jul 16 '19 at 13:12
  • I got - undefined method `with_rounding_mode' for Money:Class – drifterOcean19 Jul 16 '19 at 13:18
  • `Money.with_rounding_mode` was [added to version `6.13.3`](https://github.com/RubyMoney/money/blob/master/CHANGELOG.md#6133) of the `money` gem. Unless you say otherwise, I'm going to assume you're using the latest version of the gem. In older versions, it seems the method was called `Money.with_rounding_mode`. – Tom Lord Jul 16 '19 at 13:25
  • I tried that, it didn't work either. Still giving me £785 – drifterOcean19 Jul 16 '19 at 13:36
  • *`Money.rounding_mode`. (Shame SO doesn't let me edit comments :( ... ) – Tom Lord Jul 16 '19 at 13:51

0 Answers0