最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far