HTML5 Number Input - Always show 2 decimal places

后端 未结 15 2182
太阳男子
太阳男子 2020-11-28 05:43

Is there\'s any way to format an input[type=\'number\'] value to always show 2 decimal places?

Example: I want to see 0.00 instead of

15条回答
  •  余生分开走
    2020-11-28 06:38

    import { Component, Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
        name: 'replace'
    })
    
    export class ReplacePipe implements PipeTransform {
        transform(value: any): any {
            value = String(value).toString();
            var afterPoint = '';
            var plus = ',00';
            if (value.length >= 4) {
                if (value.indexOf('.') > 0) {
                    afterPoint = value.substring(value.indexOf('.'), value.length);
                    var te = afterPoint.substring(0, 3);
                    if (te.length == 2) {
                        te = te + '0';
                    }
                }
                if (value.indexOf('.') > 0) {
                    if (value.indexOf('-') == 0) {
                        value = parseInt(value);
                        if (value == 0) {
                            value = '-' + value + te;
                            value = value.toString();
                        }
                        else {
                            value = value + te;
                            value = value.toString();
                        }
                    }
                    else {
                        value = parseInt(value);
                        value = value + te;
                        value = value.toString();
                    }
                }
                else {
                    value = value.toString() + plus;
                }
                var lastTwo = value.substring(value.length - 2);
                var otherNumbers = value.substring(0, value.length - 3);
                if (otherNumbers != '')
                    lastTwo = ',' + lastTwo;
                let newValue = otherNumbers.replace(/\B(?=(\d{3})+(?!\d))/g, ".") + lastTwo;
                parseFloat(newValue);
                return `${newValue}`;
            }
    }
    }
    

提交回复
热议问题