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

javascript - How can I format the text entered into an HTML textfield like currency? - Stack Overflow

programmeradmin0浏览0评论

I have a series of texfields, which I'd like to format as currency. Preferably, this would be done on the fly but at least onblur. What I mean by currency format is 349507 -> $349,507. Is it possible?

I prefer HTML/CSS/JS solutions, because I need less explanation. I'm not familiar at all with jQuery.

Any help is greatly appreciated.
Mike

I have a series of texfields, which I'd like to format as currency. Preferably, this would be done on the fly but at least onblur. What I mean by currency format is 349507 -> $349,507. Is it possible?

I prefer HTML/CSS/JS solutions, because I need less explanation. I'm not familiar at all with jQuery.

Any help is greatly appreciated.
Mike

Share Improve this question asked Jul 22, 2009 at 2:06 Michael SwartsMichael Swarts 3,9218 gold badges36 silver badges46 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

Here's a some code I wrote a long while ago to format a number with commas. An example is formatNumber(349507, 0, 2, true)"349,507.00".

// Reformats a number by inserting commas and padding out the number of digits
// and decimal places.
//
// Parameters:
//     number:        The number to format. All non-numeric characters are
//                    stripped out first.
//     digits:        The minimum number of digits to the left of the decimal
//                    point. The extra places are padded with zeros.
//     decimalPlaces: The number of places after the decimal point, or zero to
//                    omit the decimal point.
//     withCommas:    True to insert commas every 3 places, false to omit them.
function formatNumber(number, digits, decimalPlaces, withCommas)
{
        number       = number.toString();
    var simpleNumber = '';

    // Strips out the dollar sign and commas.
    for (var i = 0; i < number.length; ++i)
    {
        if ("0123456789.".indexOf(number.charAt(i)) >= 0)
            simpleNumber += number.charAt(i);
    }

    number = parseFloat(simpleNumber);

    if (isNaN(number))      number     = 0;
    if (withCommas == null) withCommas = false;
    if (digits     == 0)    digits     = 1;

    var integerPart = (decimalPlaces > 0 ? Math.floor(number) : Math.round(number));
    var string      = "";

    for (var i = 0; i < digits || integerPart > 0; ++i)
    {
        // Insert a comma every three digits.
        if (withCommas && string.match(/^\d\d\d/))
            string = "," + string;

        string      = (integerPart % 10) + string;
        integerPart = Math.floor(integerPart / 10);
    }

    if (decimalPlaces > 0)
    {
        number -= Math.floor(number);
        number *= Math.pow(10, decimalPlaces);

        string += "." + formatNumber(number, decimalPlaces, 0);
    }

    return string;
}

You can use it on an onblur event handler like so:

<input type="text" onblur="this.value = '$' + formatNumber(this.value, 0, 0, true)" />

That'll add commas to the number and slap a dollar sign on the front.

First result in a Google search for "javascript format currency"

http://www.web-source.net/web_development/currency_formatting.htm

function CurrencyFormatted(amount)
{
    var i = parseFloat(amount);
    if(isNaN(i)) { i = 0.00; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}
发布评论

评论列表(0)

  1. 暂无评论