# [Vue.js] Decimal Formatting in Vue Framework

when new to vue.js Framework. My requirement is to add money currency formatting in an input box.

Formatting:
I need to add a decimal with two zeros on focus out and remove zero at focus in. The v-modal value should not change as this format is just for the user display.

I found this solution which is quite close to my requirement: https://jsfiddle.net/mani04/w6oo9b6j. There are just two additional things when not able to implement.

to use it like:

<my-currency-input id=”test1” name=”test2” v-model=”price” v-validate=”required|numeric ></my-currency-input>

there is added vee-validate.js lib for validation for my whole form. So how can I use v-validate in this example?
I don’t want to round off my string. We just need to add and remove (.00)
Here, if user enters 35.7896 it is making it 35.79. For my requirement, it should remain 35.7896 as it is already having decimal. It should only add decimal when user enter a number.

How can I do this? Should I use vue.js directive for this?

https://jsfiddle.net/mani04/w6oo9b6j/

Something like this I want

<my-currency-input id=”test1” name=”test2” v-model=”price” v-validate=”required|numeric ></my-currency-input>

### Solution :

You don’t need a focus-out/focus-in method. What you need is a computed property.
Try this out:

Vue.component(‘my-currency-input’, {
template: 
<div>
<input type=”text” v-model=”currencyValue” /> { formattedCurrencyValue }
</div>,
data: function() {
return {
currencyValue: 0,
/* formattedCurrencyValue: “$0.00” */ } }, computed: { formattedCurrencyValue: function(){ if(!this.currencyValue){ return “$0.00”}
return “\$” + parseFloat(this.currencyValue).toFixed(2)
}
}
});

new Vue({
el: ‘#app’
});