diff --git a/atst/filters.py b/atst/filters.py index 6eeb4229..61c42f8c 100644 --- a/atst/filters.py +++ b/atst/filters.py @@ -12,14 +12,6 @@ def iconSvg(name): def dollars(value): - try: - numberValue = float(value) - except ValueError: - numberValue = 0 - return "${:,.0f}".format(numberValue) - - -def dollarsWithCents(value): try: numberValue = float(value) except ValueError: @@ -107,7 +99,6 @@ def normalizeOrder(title): def register_filters(app): app.jinja_env.filters["iconSvg"] = iconSvg app.jinja_env.filters["dollars"] = dollars - app.jinja_env.filters["dollarsWithCents"] = dollarsWithCents app.jinja_env.filters["usPhone"] = usPhone app.jinja_env.filters["readableInteger"] = readableInteger app.jinja_env.filters["getOptionLabel"] = getOptionLabel diff --git a/js/components/text_input.js b/js/components/text_input.js index e5f21e4c..002884a2 100644 --- a/js/components/text_input.js +++ b/js/components/text_input.js @@ -1,5 +1,6 @@ import MaskedInput, { conformToMask } from 'vue-text-mask' import inputValidations from '../lib/input_validations' +import { formatDollars } from '../lib/dollars' export default { name: 'textinput', @@ -78,6 +79,9 @@ export default { onChange: function(e) { // Only invalidate the field when it blurs this._checkIfValid({ value: e.target.value, invalidate: true }) + if (this.validation === 'dollars') { + this.value = formatDollars(this._rawValue(e.target.value)) + } }, // diff --git a/js/lib/dollars.js b/js/lib/dollars.js index a65e5767..5ef65828 100644 --- a/js/lib/dollars.js +++ b/js/lib/dollars.js @@ -1,8 +1,14 @@ export const formatDollars = (value, cents = true) => { if (typeof value === 'number') { - return cents - ? `$${value.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')}` - : `$${value.toFixed(0).replace(/\d(?=(\d{3})+(?!\d))/g, '$&,')}` + return value.toLocaleString('us-US', { + style: 'currency', + currency: 'USD', + }) + } else if (typeof value === 'string') { + return parseFloat(value).toLocaleString('us-US', { + style: 'currency', + currency: 'USD', + }) } return '' } diff --git a/templates/task_orders/new/review.html b/templates/task_orders/new/review.html index e10d606b..65d4cc5a 100644 --- a/templates/task_orders/new/review.html +++ b/templates/task_orders/new/review.html @@ -154,7 +154,7 @@