Open In App
Related Articles

Convert decimal point numbers to percentage using filters in Vue.js

Improve Article
Save Article
Like Article

Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries.

Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is the one that’s actually printed in the Vue.js template.

The conversion from a decimal number to percentage values can be done using filters. The logic of the filter will first check if the number is less than or equal to 1. When the user provides a number that is greater than 1, a message is displayed asking the user to enter a valid number, otherwise, we will multiply the number by 100 and return it by appending a percentage sign (%).


    <script src=
    <div id='parent'>
        <p><strong>Decimal1: </strong>
        <p><strong>Percentage : </strong>
          {{ dec1 | percent }}
        <p><strong>Decimal2: </strong>
        <p><strong>Percentage : </strong>
          {{ dec2 | percent }}
        <p><strong>Decimal3: </strong>
        <p><strong>Percentage : </strong>
          {{ dec3 | percent }}
        <p><strong>Decimal4: </strong>
        <p><strong>Percentage : </strong>
          {{ dec4 | percent }}
    <script src='app.js'></script>


const parent = new Vue({
    el: '#parent',
    data: {
        dec1: 0.1,
        dec2: 0.023,
        dec3: 0.47,
        dec4: 13
    filters: {
        percent: function(dec) {
            if (dec <= 1) {
                return dec * 100 + "%";
            return 'Please enter number less than or equal to 1'


Decimal to percentage using filter

Last Updated : 17 Mar, 2021
Like Article
Save Article
Similar Reads
Related Tutorials