Step 1
Create a file called filters.js.
This is from one of my projects with locale changed to fr and currency changed to CFA (FCFA didn't work when I tested).
Note you can use another French based locale, such as fr-CF for a Central African Republic French locale. As I don't know where you, or your project's client, is from, I choose to use just fr.
import Vue from 'vue';
const integerFormatter = new Intl.NumberFormat('fr', {
maximumFractionDigits: 0,
});
const decimalFormatter = new Intl.NumberFormat('fr', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
const percentageFormatter = new Intl.NumberFormat('fr', {
style: 'percent',
minimumFractionDigits: 1,
maximumFractionDigits: 1,
});
const currencyFormatter = new Intl.NumberFormat('fr', {
style: 'currency',
currency: 'CFA',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
Vue.filter('floor', function floorFilter(value) {
const number = Number(value);
return Number.isFinite(number) ? Math.floor(number) : null;
});
Vue.filter('integer', function integerFilter(value) {
const number = Number(value);
if (!Number.isFinite(number)) {
return null;
}
return integerFormatter.format(number);
});
Vue.filter('percentage', function percentageFilter(value) {
const number = Number(value);
if (!Number.isFinite(number)) {
return null;
}
return percentageFormatter.format(number);
});
Vue.filter('decimal', function decimalFilter(value) {
const number = Number(value);
if (!Number.isFinite(number)) {
return null;
}
return decimalFormatter.format(number);
});
Vue.filter('currency', function currencyFilter(value) {
const number = Number(value);
if (!Number.isFinite(number)) {
return null;
}
return currencyFormatter.format(number);
});
This will register 5 new global filters within Vue: floor, integer, percentage, decimal, and currency.
Note I used some defaults based on the project I got this snippet from. If your locale uses 3 decimal places (some places do), or any other value, you can customize those options.
Thousands separators will be added according to the chosen locale.
Step 2
But for them to be available on your components you need to import this file in your entry point.
On a regular Vue app using the Laravel included files, this would be your ./resources/js/app.js.
Import the filters file there:
import Vue from 'vue';
// other imports, if any
import './filters.js';
// other plugin initialization, if any
window.app = new Vue({
el: '#app',
// ... other root element code
});
Here I am assuming you saved the filters.js file in the same folder as your app.js file.
Step 3
Now you can use these new filters in your components by using the pipe | operator:
<div class="item-description">
<h4>{{ produit.nom }}</h4>
<p>{{ produit.prix | integer }} FCFA</p>
</div>
Here I used the integer filter, but you can experiment with the other ones. The currency filter will already append (or prepend, depending on the locale) the currency symbol. But as in your template you are using FCFA instead of just CFA maybe you'd prefer to use the integer or decimal ones.
Step 4 & References
Compile your code and test your changes.
Note that filters is one Vue feature that was removed from version 3 (I wish they wouldn't, they are so useful).
So if you are using Vue 3 you can add those filters as a component's methods and call them as regular methods.
I added a link to Vue 3 migration guide about how to "migrate" filters from Vue 2 to Vue 3.
References:
https://vuejs.org/v2/guide/filters.html
https://v3.vuejs.org/guide/migration/filters.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
https://www.fincher.org/Utilities/CountryLanguageList.shtml
http://www.lingoes.net/en/translator/langcode.htm
Hope it helps.