Skip to content

Instantly share code, notes, and snippets.

@leopolicastro
Last active May 20, 2022 02:47
Show Gist options
  • Save leopolicastro/cc67299dfdeed22ce3dd0d52891a18c4 to your computer and use it in GitHub Desktop.
Save leopolicastro/cc67299dfdeed22ce3dd0d52891a18c4 to your computer and use it in GitHub Desktop.
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
static values = {
maxallowed: Number,
};
currencyFormatter(e) {
let value = e.target.value;
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
});
// remove all currency formatting
value = parseFloat(
value
.replaceAll(",", "")
.replace("$", "")
.replace(".00", "")
.replace(".", "")
);
e.target.value = formatter.format(value / 100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment