v-money icon indicating copy to clipboard operation
v-money copied to clipboard

Is there any way to retrieve the numeric value ?

Open leonardojobim opened this issue 7 years ago • 6 comments

The v-model returns a string with formatted value when using directives. Is there any way to retrieve the numeric value ?

leonardojobim avatar Feb 06 '18 17:02 leonardojobim

let number = Number(textValue)

paulpv avatar Apr 03 '18 19:04 paulpv

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?

Christilut avatar May 20 '18 11:05 Christilut

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

SrAxi avatar Oct 12 '18 13:10 SrAxi

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.

zacharytyhacz avatar Nov 21 '19 23:11 zacharytyhacz

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?

DiogoBotton avatar Jun 30 '21 19:06 DiogoBotton

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?

pccortez avatar Aug 26 '22 15:08 pccortez