首页 文章

在JavaScript中格式化一个正好两位小数的数字

提问于
浏览
524

我有这行代码将我的数字四舍五入到小数点后两位 . 但是我得到这样的数字:10.8,2.4等 . 这些不是我的两位小数的想法,所以我如何改进以下内容?

Math.round(price*Math.pow(10,2))/Math.pow(10,2);

我想要像10.80,2.40等数字 . 使用jQuery对我来说很好 .

29 回答

  • 0

    Round down

    function round_down(value, decPlaces) {
        return Math.floor(value * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
    }
    

    Round up

    function round_up(value, decPlaces) {
        return Math.ceil(value * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
    }
    

    Round nearest

    function round_nearest(value, decPlaces) {
        return Math.round(value * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
    }
    

    合并https://stackoverflow.com/a/7641824/1889449和https://www.kirupa.com/html5/rounding_numbers_in_javascript.htm谢谢他们 .

  • 2

    要使用定点表示法格式化数字,只需使用toFixed方法:

    (10.8).toFixed(2); // "10.80"
    
    var num = 2.4;
    alert(num.toFixed(2)); // "2.40"
    

    请注意 toFixed() 返回一个字符串 .

    IMPORTANT :请注意,toFixed实际上并不是圆形的,90%的时间它会返回舍入值,但在很多情况下它实际上并不起作用 . 在您的控制台中试试这个:

    2.005.toFixed(2)

    你会得到错误的答案

    在javascript中没有自然的方式来获得小数舍入,您将需要自己的polyfill或使用库 . 你可以看一下mozilla的polyfill https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round

  • 1

    这是一个古老的主题,但仍然排名靠前的谷歌搜索结果和提供的解决方案共享相同的浮点小数问题 . 这是我使用的(非常通用)函数,thanks to MDN

    function round(value, exp) {
      if (typeof exp === 'undefined' || +exp === 0)
        return Math.round(value);
    
      value = +value;
      exp = +exp;
    
      if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
        return NaN;
    
      // Shift
      value = value.toString().split('e');
      value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)));
    
      // Shift back
      value = value.toString().split('e');
      return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
    }
    

    我们可以看到,我们没有遇到这些问题:

    round(1.275, 2);   // Returns 1.28
    round(1.27499, 2); // Returns 1.27
    

    这种通用性也提供了一些很酷的东西:

    round(1234.5678, -2);   // Returns 1200
    round(1.2345678e+2, 2); // Returns 123.46
    round("123.45");        // Returns 123
    

    现在,要回答OP的问题,必须输入:

    round(10.8034, 2).toFixed(2); // Returns "10.80"
    round(10.8, 2).toFixed(2);    // Returns "10.80"
    

    或者,为了更简洁,更通用的功能:

    function round2Fixed(value) {
      value = +value;
    
      if (isNaN(value))
        return NaN;
    
      // Shift
      value = value.toString().split('e');
      value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + 2) : 2)));
    
      // Shift back
      value = value.toString().split('e');
      return (+(value[0] + 'e' + (value[1] ? (+value[1] - 2) : -2))).toFixed(2);
    }
    

    你可以用它来调用它:

    round2Fixed(10.8034); // Returns "10.80"
    round2Fixed(10.8);    // Returns "10.80"
    

    各种示例和测试(感谢@t-j-crowder!):

    function round(value, exp) {
      if (typeof exp === 'undefined' || +exp === 0)
        return Math.round(value);
    
      value = +value;
      exp = +exp;
    
      if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
        return NaN;
    
      // Shift
      value = value.toString().split('e');
      value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)));
    
      // Shift back
      value = value.toString().split('e');
      return +(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp));
    }
    function naive(value, exp) {
      if (!exp) {
        return Math.round(value);
      }
      var pow = Math.pow(10, exp);
      return Math.round(value * pow) / pow;
    }
    function test(val, places) {
      subtest(val, places);
      val = typeof val === "string" ? "-" + val : -val;
      subtest(val, places);
    }
    function subtest(val, places) {
      var placesOrZero = places || 0;
      var naiveResult = naive(val, places);
      var roundResult = round(val, places);
      if (placesOrZero >= 0) {
        naiveResult = naiveResult.toFixed(placesOrZero);
        roundResult = roundResult.toFixed(placesOrZero);
      } else {
        naiveResult = naiveResult.toString();
        roundResult = roundResult.toString();
      }
      $("<tr>")
        .append($("<td>").text(JSON.stringify(val)))
        .append($("<td>").text(placesOrZero))
        .append($("<td>").text(naiveResult))
        .append($("<td>").text(roundResult))
        .appendTo("#results");
    }
    test(0.565, 2);
    test(0.575, 2);
    test(0.585, 2);
    test(1.275, 2);
    test(1.27499, 2);
    test(1234.5678, -2);
    test(1.2345678e+2, 2);
    test("123.45");
    test(10.8034, 2);
    test(10.8, 2);
    test(1.005, 2);
    test(1.0005, 2);
    
    table {
      border-collapse: collapse;
    }
    table, td, th {
      border: 1px solid #ddd;
    }
    td, th {
      padding: 4px;
    }
    th {
      font-weight: normal;
      font-family: sans-serif;
    }
    td {
      font-family: monospace;
    }
    
    <table>
      <thead>
        <tr>
          <th>Input</th>
          <th>Places</th>
          <th>Naive</th>
          <th>Thorough</th>
        </tr>
      </thead>
      <tbody id="results">
      </tbody>
    </table>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
  • 0

    我通常将它添加到我的个人库中,经过一些建议并使用@TIMINeutron解决方案,然后使其适应小数长度,这个最适合:

    function precise_round(num, decimals) {
       var t = Math.pow(10, decimals);   
       return (Math.round((num * t) + (decimals>0?1:0)*(Math.sign(num) * (10 / Math.pow(100, decimals)))) / t).toFixed(decimals);
    }
    

    将适用于所报告的例外情况 .

  • 2

    我不知道为什么我不能在之前的答案中添加评论(也许我绝对是盲目的,不知道),但我想出了一个使用@Miguel答案的解决方案:

    function precise_round(num,decimals) {
       return Math.round(num*Math.pow(10, decimals)) / Math.pow(10, decimals);
    }
    

    它的两条评论(来自@bighostkim和@Imre):

    • 问题 precise_round(1.275,2) 未返回1.28

    • 问题 precise_round(6,2) 未返回6.00(正如他所想) .

    我的最终解决方案如下:

    function precise_round(num,decimals) {
        var sign = num >= 0 ? 1 : -1;
        return (Math.round((num*Math.pow(10,decimals)) + (sign*0.001)) / Math.pow(10,decimals)).toFixed(decimals);
    }
    

    正如你所看到的,我不得不添加一点"correction"(它不是它的原因,但是因为Math.round是有损的 - 你可以在jsfiddle.net上查看它 - 这是我知道如何"fix"它的唯一方法) . 它将已填充的数字加0.001,因此它在十进制值的右侧添加 1 三个 0 . 所以它应该是安全的 .

    之后我添加 .toFixed(decimal) 以始终以正确的格式输出数字(使用正确的小数) .

    所以这就是它 . 好好用;)

    编辑:为负数的“修正”增加了功能 .

  • 5

    一种方法是100%确定你得到一个2位小数的数字:

    (Math.round(num*100)/100).toFixed(2)
    

    如果这导致舍入错误,您可以使用以下内容,正如詹姆斯在他的评论中所解释的那样:

    (Math.round((num * 1000)/10)/100).toFixed(2)
    
  • 1

    toFixed(n)在小数点后提供n个长度; toPrecision(x)提供x总长度 .

    请使用以下方法

    // Example: toPrecision(4) when the number has 7 digits (3 before, 4 after)
        // It will round to the tenths place
        num = 500.2349;
        result = num.toPrecision(4); // result will equal 500.2
    

    如果你想要数字固定使用

    result = num.toFixed(2);
    
  • 41

    使用这些示例,在尝试舍入数字1.005时仍会出现错误,解决方案是使用像Math.js这样的库或此函数:

    function round(value: number, decimals: number) {
        return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
    }
    
  • 16

    这是我的1行解决方案: Number((yourNumericValueHere).toFixed(2));

    这是发生的事情:

    1)首先,将 .toFixed(2) 应用于要舍入小数位的数字 . 请注意,这会将值从数字转换为字符串 . 因此,如果您使用的是Typescript,它将抛出如下错误:

    “类型'字符串'不能指定为'数字'类型”

    2)要获取数值或将字符串转换为数值,只需在所谓的'string'值上应用 Number() 函数 .

    有关说明,请查看以下示例:

    EXAMPLE: 我的小数位数最多有5位数,我想把它缩短到2位小数 . 我是这样做的:

    var price = 0.26453;
    var priceRounded = Number((price).toFixed(2));
    console.log('Original Price: ' + price);
    console.log('Price Rounded: ' + priceRounded);
    
  • 1

    我找不到这个问题的准确解决方案,所以我创建了自己的:

    function inprecise_round(value, decPlaces) {
      return Math.round(value*Math.pow(10,decPlaces))/Math.pow(10,decPlaces);
    }
    
    function precise_round(value, decPlaces){
        var val = value * Math.pow(10, decPlaces);
        var fraction = (Math.round((val-parseInt(val))*10)/10);
    
        //this line is for consistency with .NET Decimal.Round behavior
        // -342.055 => -342.06
        if(fraction == -0.5) fraction = -0.6;
    
        val = Math.round(parseInt(val) + fraction) / Math.pow(10, decPlaces);
        return val;
    }
    

    例子:

    function inprecise_round(value, decPlaces) {
      return Math.round(value * Math.pow(10, decPlaces)) / Math.pow(10, decPlaces);
    }
    
    function precise_round(value, decPlaces) {
      var val = value * Math.pow(10, decPlaces);
      var fraction = (Math.round((val - parseInt(val)) * 10) / 10);
    
      //this line is for consistency with .NET Decimal.Round behavior
      // -342.055 => -342.06
      if (fraction == -0.5) fraction = -0.6;
    
      val = Math.round(parseInt(val) + fraction) / Math.pow(10, decPlaces);
      return val;
    }
    
    // This may produce different results depending on the browser environment
    console.log("342.055.toFixed(2)         :", 342.055.toFixed(2)); // 342.06 on Chrome & IE10
    
    console.log("inprecise_round(342.055, 2):", inprecise_round(342.055, 2)); // 342.05
    console.log("precise_round(342.055, 2)  :", precise_round(342.055, 2));   // 342.06
    console.log("precise_round(-342.055, 2) :", precise_round(-342.055, 2));  // -342.06
    
    console.log("inprecise_round(0.565, 2)  :", inprecise_round(0.565, 2));   // 0.56
    console.log("precise_round(0.565, 2)    :", precise_round(0.565, 2));     // 0.57
    
  • 83

    @heridev和我在jQuery中创建了一个小函数 .

    您可以尝试下一个:

    HTML

    <input type="text" name="one" class="two-digits"><br>
    <input type="text" name="two" class="two-digits">​
    

    jQuery的

    // apply the two-digits behaviour to elements with 'two-digits' as their class
    $( function() {
        $('.two-digits').keyup(function(){
            if($(this).val().indexOf('.')!=-1){         
                if($(this).val().split(".")[1].length > 2){                
                    if( isNaN( parseFloat( this.value ) ) ) return;
                    this.value = parseFloat(this.value).toFixed(2);
                }  
             }            
             return this; //for chaining
        });
    });
    

    在线演示:

    http://jsfiddle.net/c4Wqn/

  • 1

    浮点值的问题在于它们试图用固定的位数表示无限量的(连续)值 . 所以很自然地,游戏中肯定会有一些损失,你会被一些 Value 观所困扰 .

    当计算机将1.275存储为浮点值时,它赢得了't actually remember whether it was 1.275 or 1.27499999999999993, or even 1.27500000000000002. These values should give different results after rounding to two decimals, but they won't,因为对于计算机,它们在存储为浮点值后看起来完全相同,并且无法恢复丢失的数据 . 任何进一步的计算只会累积这种不精确性 .

    因此,如果精度很重要,则必须从一开始就避免使用浮点值 . 最简单的选择是

    • 使用devoted library

    • 使用字符串存储和传递值(伴随字符串操作)

    • 使用整数(例如,您可以传递实际值的百分之一,例如以美分而不是以美元计算的金额)

    例如,当使用整数来存储百分之一时,查找实际值的函数非常简单:

    function descale(num, decimals) {
        var hasMinus = num < 0;
        var numString = Math.abs(num).toString();
        var precedingZeroes = '';
        for (var i = numString.length; i <= decimals; i++) {
            precedingZeroes += '0';
        }
        numString = precedingZeroes + numString;
        return (hasMinus ? '-' : '') 
            + numString.substr(0, numString.length-decimals) 
            + '.' 
            + numString.substr(numString.length-decimals);
    }
    
    alert(descale(127, 2));
    

    使用字符串,您需要舍入,但它仍然可以管理:

    function precise_round(num, decimals) {
        var parts = num.split('.');
        var hasMinus = parts.length > 0 && parts[0].length > 0 && parts[0].charAt(0) == '-';
        var integralPart = parts.length == 0 ? '0' : (hasMinus ? parts[0].substr(1) : parts[0]);
        var decimalPart = parts.length > 1 ? parts[1] : '';
        if (decimalPart.length > decimals) {
            var roundOffNumber = decimalPart.charAt(decimals);
            decimalPart = decimalPart.substr(0, decimals);
            if ('56789'.indexOf(roundOffNumber) > -1) {
                var numbers = integralPart + decimalPart;
                var i = numbers.length;
                var trailingZeroes = '';
                var justOneAndTrailingZeroes = true;
                do {
                    i--;
                    var roundedNumber = '1234567890'.charAt(parseInt(numbers.charAt(i)));
                    if (roundedNumber === '0') {
                        trailingZeroes += '0';
                    } else {
                        numbers = numbers.substr(0, i) + roundedNumber + trailingZeroes;
                        justOneAndTrailingZeroes = false;
                        break;
                    }
                } while (i > 0);
                if (justOneAndTrailingZeroes) {
                    numbers = '1' + trailingZeroes;
                }
                integralPart = numbers.substr(0, numbers.length - decimals);
                decimalPart = numbers.substr(numbers.length - decimals);
            }
        } else {
            for (var i = decimalPart.length; i < decimals; i++) {
                decimalPart += '0';
            }
        }
        return (hasMinus ? '-' : '') + integralPart + (decimals > 0 ? '.' + decimalPart : '');
    }
    
    alert(precise_round('1.275', 2));
    alert(precise_round('1.27499999999999993', 2));
    

    请注意,此函数舍入到最接近,与零相关,而IEEE 754建议舍入到最近,甚至作为浮点运算的默认行为 . 这些修改留给读者练习:)

  • 13

    这是一个简单的

    function roundFloat(num,dec){
        var d = 1;
        for (var i=0; i<dec; i++){
            d += "0";
        }
        return Math.round(num * d) / d;
    }
    

    alert(roundFloat(1.79209243929,4)); 一样使用

    Jsfiddle

  • 0

    舍入小数值,然后使用 toFixed(x) 作为预期的数字 .

    function parseDecimalRoundAndFixed(num,dec){
      var d =  Math.pow(10,dec);
      return (Math.round(num * d) / d).toFixed(dec);
    }
    

    呼叫

    parseDecimalRoundAndFixed(10.800243929,4)=> 10.80 parseDecimalRoundAndFixed(10.807243929,2)=>10.81

  • 1

    the following 放在某个全局范围内:

    Number.prototype.getDecimals = function ( decDigCount ) {
       return this.toFixed(decDigCount);
    }
    

    then try

    var a = 56.23232323;
    a.getDecimals(2); // will return 56.23
    

    更新

    请注意 toFixed() 只能用于_919950之间的小数位数,即 a.getDecimals(25) 可能会生成javascript错误,因此为了适应您可以添加一些额外的检查,即

    Number.prototype.getDecimals = function ( decDigCount ) {
       return ( decDigCount > 20 ) ? this : this.toFixed(decDigCount);
    }
    
  • 1
    Number(((Math.random() * 100) + 1).toFixed(2))
    

    这将返回1到100的随机数,四舍五入到小数点后两位 .

  • 0
    Number(Math.round(1.005+'e2')+'e-2'); // 1.01
    

    这对我有用:Rounding Decimals in JavaScript

  • 0

    通过引用使用此响应:https://stackoverflow.com/a/21029698/454827

    我构建一个函数来获得动态的小数:

    function toDec(num, dec)
    {
            if(typeof dec=='undefined' || dec<0)
                    dec = 2;
    
            var tmp = dec + 1;
            for(var i=1; i<=tmp; i++)
                    num = num * 10;
    
            num = num / 10;
            num = Math.round(num);
            for(var i=1; i<=dec; i++)
                    num = num / 10;
    
            num = num.toFixed(dec);
    
            return num;
    }
    

    这里的工作示例:https://jsfiddle.net/wpxLduLc/

  • 1
    parse = function (data) {
           data = Math.round(data*Math.pow(10,2))/Math.pow(10,2);
           if (data != null) {
                var lastone = data.toString().split('').pop();
                if (lastone != '.') {
                     data = parseFloat(data);
                }
           }
           return data;
      };
    
    $('#result').html(parse(200)); // output 200
    $('#result1').html(parse(200.1)); // output 200.1
    $('#result2').html(parse(200.10)); // output 200.1
    $('#result3').html(parse(200.109)); // output 200.11
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <div id="result"></div>
    <div id="result1"></div>
    <div id="result2"></div>
    <div id="result3"></div>
    
  • 2

    几个月前我从这篇文章中得到了一些想法,但这里没有答案,也没有其他帖子/博客的答案可以处理所有场景(例如我们的测试人员发现的负数和一些“幸运数字”) . 最后,我们的测试人员发现以下方法没有任何问题 . 粘贴我的代码片段:

    fixPrecision: function (value) {
        var me = this,
            nan = isNaN(value),
            precision = me.decimalPrecision;
    
        if (nan || !value) {
            return nan ? '' : value;
        } else if (!me.allowDecimals || precision <= 0) {
            precision = 0;
        }
    
        //[1]
        //return parseFloat(Ext.Number.toFixed(parseFloat(value), precision));
        precision = precision || 0;
        var negMultiplier = value < 0 ? -1 : 1;
    
        //[2]
        var numWithExp = parseFloat(value + "e" + precision);
        var roundedNum = parseFloat(Math.round(Math.abs(numWithExp)) + 'e-' + precision) * negMultiplier;
        return parseFloat(roundedNum.toFixed(precision));
    },
    

    我也有代码评论(对不起,我已经忘记了所有细节)...我在这里发布我的答案以供将来参考:

    9.995 * 100 = 999.4999999999999
    Whereas 9.995e2 = 999.5
    This discrepancy causes Math.round(9.995 * 100) = 999 instead of 1000.
    Use e notation instead of multiplying /dividing by Math.Pow(10,precision).
    
  • 1

    我解决了修饰符的问题 . Support 2 decimal only.

    $(function(){
      //input number only.
      convertNumberFloatZero(22); // output : 22.00
      convertNumberFloatZero(22.5); // output : 22.50
      convertNumberFloatZero(22.55); // output : 22.55
      convertNumberFloatZero(22.556); // output : 22.56
      convertNumberFloatZero(22.555); // output : 22.55
      convertNumberFloatZero(22.5541); // output : 22.54
      convertNumberFloatZero(22222.5541); // output : 22,222.54
    
      function convertNumberFloatZero(number){
    	if(!$.isNumeric(number)){
    		return 'NaN';
    	}
    	var numberFloat = number.toFixed(3);
    	var splitNumber = numberFloat.split(".");
    	var cNumberFloat = number.toFixed(2);
    	var cNsplitNumber = cNumberFloat.split(".");
    	var lastChar = splitNumber[1].substr(splitNumber[1].length - 1);
    	if(lastChar > 0 && lastChar < 5){
    		cNsplitNumber[1]--;
    	}
    	return Number(splitNumber[0]).toLocaleString('en').concat('.').concat(cNsplitNumber[1]);
      };
    });
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
  • -4
    (Math.round((10.2)*100)/100).toFixed(2)
    

    那会产生: 10.20

    (Math.round((.05)*100)/100).toFixed(2)
    

    应该产生: 0.05

    (Math.round((4.04)*100)/100).toFixed(2)
    

    那会产生: 4.04

    等等

  • 914
    /*Due to all told stuff. You may do 2 things for different purposes:
    When showing/printing stuff use this in your alert/innerHtml= contents:
    YourRebelNumber.toFixed(2)*/
    
    var aNumber=9242.16;
    var YourRebelNumber=aNumber-9000;
    alert(YourRebelNumber);
    alert(YourRebelNumber.toFixed(2));
    
    /*and when comparing use:
    Number(YourRebelNumber.toFixed(2))*/
    
    if(YourRebelNumber==242.16)alert("Not Rounded");
    if(Number(YourRebelNumber.toFixed(2))==242.16)alert("Rounded");
    
    /*Number will behave as you want in that moment. After that, it'll return to its defiance.
    */
    
  • 0

    这非常简单,并且与其他任何一个一样有效:

    function parseNumber(val, decimalPlaces) {
        if (decimalPlaces == null) decimalPlaces = 0
        var ret = Number(val).toFixed(decimalPlaces)
        return Number(ret)
    }
    

    由于toFixed()只能在数字上调用,并且不幸地返回一个字符串,因此它会在两个方向上为您完成所有解析 . 你可以传递一个字符串或一个数字,每次都会得到一个数字!调用parseNumber(1.49)会给你1,而parseNumber(1.49,2)会给你1.50 . 就像他们最好的一样!

  • 0

    您也可以使用 .toPrecision() 方法和一些自定义代码,并且无论int部分的长度如何,始终向上舍入到第n个十进制数字 .

    function glbfrmt (number, decimals, seperator) {
        return typeof number !== 'number' ? number : number.toPrecision( number.toString().split(seperator)[0].length + decimals);
    }
    

    你也可以把它作为一个插件,以便更好地使用 .

  • 12
    /**
     * MidpointRounding away from zero ('arithmetic' rounding)
     * Uses a half-epsilon for correction. (This offsets IEEE-754
     * half-to-even rounding that was applied at the edge cases).
     */
    
    function RoundCorrect(num, precision = 2) {
    	// half epsilon to correct edge cases.
    	var c = 0.5 * Number.EPSILON * num;
    //	var p = Math.pow(10, precision); //slow
    	var p = 1; while (precision--> 0) p *= 10;
    	if (num < 0)
    		p *= -1;
    	return Math.round((num + c) * p) / p;
    }
    
    // testing some +ve edge cases
    console.log(RoundCorrect(1.005, 2));  // 1.01 correct
    console.log(RoundCorrect(2.175, 2));  // 2.18 correct
    console.log(RoundCorrect(5.015, 2));  // 5.02 correct
    
    // testing some -ve edge cases
    console.log(RoundCorrect(-1.005, 2));  // -1.01 correct
    console.log(RoundCorrect(-2.175, 2));  // -2.18 correct
    console.log(RoundCorrect(-5.015, 2));  // -5.02 correct
    
  • 0

    很简单 .

    var rounded_value=Math.round(value * 100) / 100;
    
  • 3

    我发现了一种非常简单的方法可以解决这个问题,可以使用或改编:

    td[row].innerHTML = price.toPrecision(price.toFixed(decimals).length
    
  • 1

    100%工作!!!试试吧

    <html>
         <head>
          <script>
          function replacePonto(){
            var input = document.getElementById('qtd');
            var ponto = input.value.split('.').length;
            var slash = input.value.split('-').length;
            if (ponto > 2)
                    input.value=input.value.substr(0,(input.value.length)-1);
    
            if(slash > 2)
                    input.value=input.value.substr(0,(input.value.length)-1);
    
            input.value=input.value.replace(/[^0-9.-]/,'');
    
            if (ponto ==2)
    	input.value=input.value.substr(0,(input.value.indexOf('.')+3));
    
    if(input.value == '.')
    	input.value = "";
                  }
          </script>
          </head>
          <body>
             <input type="text" id="qtd" maxlength="10" style="width:140px" onkeyup="return replacePonto()">
          </body>
        </html>
    

相关问题