javascript - Format a textbox like "0.00" - Stack Overflow

How to format a textbox like "0.00" using jQuery or JavaScript? If I enter value 59, then it

How to format a textbox like "0.00" using jQuery or JavaScript? If I enter value 59, then it should bee 59.00. If I enter value as 59.20, then it should be the same.

How to format a textbox like "0.00" using jQuery or JavaScript? If I enter value 59, then it should bee 59.00. If I enter value as 59.20, then it should be the same.

Share Improve this question edited Nov 13, 2012 at 5:47 Peter O. 32.9k14 gold badges85 silver badges97 bronze badges asked Mar 26, 2010 at 5:55 Joby KurianJoby Kurian 3,9474 gold badges32 silver badges38 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

With jQuery, assuming you have an input like this:

<input type="text" id="someInput" name="something"/>

You can use this:

$('#someInput').blur(function() {
    var floatValue = parseFloat(this.value);
    if (!isNaN(floatValue)) { // make sure they actually entered a number
        this.value = floatValue.toFixed(2);
    }
});

Whenever the input loses focus, the value will be converted always have 2 decimal places, as long as the user entered something that can be parsed into a number.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745642694a4637787.html

相关推荐

  • javascript - Format a textbox like &quot;0.00&quot; - Stack Overflow

    How to format a textbox like "0.00" using jQuery or JavaScript? If I enter value 59, then it

    22天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信