FIX: Decimal point truncation (#223)

Amounts like $45.80 were being displayed as $45.8. Which doesn't look
correct for currency.

Bug Report: https://meta.discourse.org/t/316007
This commit is contained in:
Blake Erickson 2024-07-19 07:18:28 -06:00 committed by GitHub
parent 403c966514
commit 41c443ab23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 12 deletions

View File

@ -1,40 +1,36 @@
import Helper from "@ember/component/helper"; import { helper } from "@ember/component/helper";
export default Helper.helper(function (params) { export function formatCurrency([currency, amount]) {
let currencySign; let currencySign;
switch (params[0]) { switch (currency.toUpperCase()) {
case "EUR": case "EUR":
case "eur":
currencySign = "€"; currencySign = "€";
break; break;
case "GBP": case "GBP":
case "gbp":
currencySign = "£"; currencySign = "£";
break; break;
case "INR": case "INR":
case "inr":
currencySign = "₹"; currencySign = "₹";
break; break;
case "BRL": case "BRL":
case "brl":
currencySign = "R$"; currencySign = "R$";
break; break;
case "DKK": case "DKK":
case "dkk":
currencySign = "DKK"; currencySign = "DKK";
break; break;
case "SGD": case "SGD":
case "sgd":
currencySign = "S$"; currencySign = "S$";
break; break;
case "ZAR": case "ZAR":
case "zar":
currencySign = "R"; currencySign = "R";
break; break;
default: default:
currencySign = "$"; currencySign = "$";
} }
return currencySign + params[1]; let formattedAmount = parseFloat(amount).toFixed(2);
}); return currencySign + formattedAmount;
}
export default helper(formatCurrency);

View File

@ -0,0 +1,17 @@
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import { formatCurrency } from "discourse/plugins/discourse-subscriptions/discourse/helpers/format-currency";
module("Unit | Helper | format-currency", function (hooks) {
setupTest(hooks);
test("it formats USD correctly", function (assert) {
let result = formatCurrency(["USD", 338.2]);
assert.equal(result, "$338.20", "Formats USD correctly");
});
test("it rounds correctly", function (assert) {
let result = formatCurrency(["USD", 338.289]);
assert.equal(result, "$338.29", "Rounds correctly");
});
});