最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论