Replies: 3 comments 18 replies
-
Basically exactly like this: https://dm4t2.github.io/vue-currency-input/playground.html Choose options:
|
Beta Was this translation helpful? Give feedback.
-
If anyone finds this thread to format currency, this works perfectly how I want. @beholdr even though this appears to work, I'm not 100% if this is the proper use of preProcess. Does this look acceptable to you? (nothing stands out edge-case wise) <template>
<app-input-mask
mask="0.99"
tokens="0:\d:multiple|9:\d:optional"
:options="options"
inputmode="numeric"
/>
</template>
<script>
import AppInputMask from '@/components/AppInputMask.vue';
export default {
components: {
AppInputMask,
},
setup() {
const format = (value, cents) => (
Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: cents,
maximumFractionDigits: cents,
}).format(value)
);
const options = {
preProcess: value => (
value.replace(/[$,]/g, '')
),
postProcess: value => {
if (!value) return '';
const parts = value.split('.');
if (parts.length === 1) return format(value, 0);
if (parts[1].length === 0) return `${format(value, 0)}.`;
return format(value, parts[1].length);
},
};
return {
options,
};
},
};
</script> |
Beta Was this translation helpful? Give feedback.
-
I've reworked <script setup>
const options = {
preProcess: val => val.replace(/[$,]/g, ''),
postProcess: val => {
if (!val) return ''
const sub = 3 - (val.includes('.') ? val.length - val.indexOf('.') : 0)
return Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' })
.formatToParts(val)
.map(v => v.value)
.join('')
.slice(0, sub ? -sub : undefined)
}
}
</script>
<template>
<input v-maska:[options] data-maska="0.99" data-maska-tokens="0:\d:multiple|9:\d:optional">
</template> And there was a little bug with hooks processing, fixed in v2.1.5 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to create a currency input that works as follows: (edit: see example in next comment)
Basically I always want the $ at the front, and I don't want cents to show up unless they type
.
at which point it would fill in the centsBeta Was this translation helpful? Give feedback.
All reactions