Skip to main content

Random Notes:

  • ["HUF", "JPY", "TWD"] are currencies that do not use decimals - they should be treated as integers
  • Why do we need this? Main reason is formatting (1.00), but the other is 1 cent rounding issues:
  • in rails console, do 1.15 * 100

Frontend:

https://currency.js.org/

import * as currency from 'currency.js';

get trialPrice() {
return currency(this.props.trial.price);
}

get subTotal() {
return this.price.multiply(this.itemQuantity);
}
 get total() {
let total = this.subTotal
.subtract(this.discountAmount.value)
.add(this.tax.value)
.add(this.shipping.value);
if (total.value <= 0) return currency(0);
if (this.api !== 'Stripe') return total;
return total.value > STRIPE_MINIMUM_CHARGE ? total : currency(STRIPE_MINIMUM_CHARGE);
}

store.lineItems.total.value

Backend:

https://github.com/RubyMoney/money

require 'money'

  def convert_to_money(input, currency_code)
no_decimal_currencies = ["HUF", "JPY", "TWD"]
# BigDecimal removes ruby 1 cent bug issues
converted_input = no_decimal_currencies.include?(currency_code) ? input.to_i : BigDecimal((input.to_f * 100).to_s).round(2).to_i
Money.rounding_mode = BigDecimal::ROUND_HALF_UP
Money.new(converted_input, currency_code)
end