HTML5 Number Input - Always show 2 decimal places

后端 未结 15 2042
太阳男子
太阳男子 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:23

    This is the correct answer:

    <input type="number" step="0.01" min="-9999999999.99" max="9999999999.99"/>

    0 讨论(0)
  • 2020-11-28 06:25

    Using the step attribute will enable it. It not only determines how much it's supposed to cycle, but the allowable numbers, as well. Using step="0.01" should do the trick but this may depend on how the browser adheres to the standard.

    <input type='number' step='0.01' value='5.00'>

    0 讨论(0)
  • 2020-11-28 06:27

    The top answer gave me the solution but I didn't like that the user input was changed immediately so I added delay which in my opinion contributes to a better user experience

    var delayTimer;
    function input(ele) {
        clearTimeout(delayTimer);
        delayTimer = setTimeout(function() {
           ele.value = parseFloat(ele.value).toFixed(2).toString();
        }, 800); 
    }
    <input type='number' oninput='input(this)'>

    https://jsfiddle.net/908rLhek/1/

    0 讨论(0)
  • 2020-11-28 06:33

    If you landed here just wondering how to limit to 2 decimal places I have a native javascript solution:

    Javascript:

    function limitDecimalPlaces(e, count) {
      if (e.target.value.indexOf('.') == -1) { return; }
      if ((e.target.value.length - e.target.value.indexOf('.')) > count) {
        e.target.value = parseFloat(e.target.value).toFixed(count);
      }
    }
    

    HTML:

    <input type="number" oninput="limitDecimalPlaces(event, 2)" />
    

    Note that this cannot AFAIK, defend against this chrome bug with the number input.

    0 讨论(0)
  • 2020-11-28 06:35

    This works to enforce a max of 2 decimal places without automatically rounding to 2 places if the user isn't finished typing.

    function naturalRound(e) {
    
       let dec = e.target.value.indexOf(".")
       let tooLong = e.target.value.length > dec + 3
       let invalidNum = isNaN(parseFloat(e.target.value))
    
       if ((dec >= 0 && tooLong) || invalidNum) {
         e.target.value = e.target.value.slice(0, -1)
       }
    }
    
    0 讨论(0)
  • 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}`;
            }
    }
    }
    
    0 讨论(0)
提交回复
热议问题