v-money
v-money copied to clipboard
Is there any way to retrieve the numeric value ?
The v-model returns a string with formatted value when using directives. Is there any way to retrieve the numeric value ?
let number = Number(textValue)
If the masked value is € 10.500
(so ten thousand) then the above results in NaN and if you remove the €
beforehand, then it results in 10.5
.
How does one reliably get the original value?
const convertInputToNumber = (input) => {
const thousandFixed = input
.replace(/(kr|\$|£|€)/g, '') // getting rid of currency
.trim()
.replace(/(.+)[.,](\d+)$/g, "$1x$2") // stripping number into $1: integer and $2: decimal part and putting it together with x as decimal point
.replace(/[.,]/g, '') // getting rid of . AND ,
.replace('x', '.'); // replacing x with .
return parseFloat(thousandFixed);
};
This will output a numeric value regardless of which decimal separator you're using. Just remember to add your currency symbol to the first regex.
Example:
const test1 = 'kr 123,456.78';
console.log(convertInputToNumber(test1)); // Output: 123456.78
const test2 = '€ 3,434.32';
console.log(convertInputToNumber(test2)); // Output: 3434.32
Hey y'all, just wanted to give in my two-cents ( no pun intended ).
This minimal regex will convert the simple format of
$123,456,789.01
to
123456789.01
// amount = "$123,456,789.01"; // v-model binded value from input
amount = amount.replace(/^\W|,/g,"");
amount = parseFloat(amount);
// 123456789.01
I'm no regex ninja, but it's enough for what I need and I think many others.
When I add the value 800 to v-model for example, v-money shows the data as "8.00". But the right data would be "800". Can someone help me?
When I add the value 800 to v-model for example, v-money shows the data as "8.00". But the right data would be "800". Can someone help me?
I have the same problem, did you manage to solve it?