帝王谷资源网 Design By www.wdxyy.com
复制代码 代码如下:
<script type="text/javascript">
var format = function (number, form) {
    var forms = form.split('.'), number = '' + number, numbers = number.split('.')
        , leftnumber = numbers[0].split('')
        , exec = function (lastMatch) {
            if (lastMatch == '0' || lastMatch == '#') {
                if (leftnumber.length) {
                    return leftnumber.pop();
                } else if (lastMatch == '0') {
                    return lastMatch;
                } else {
                    return '';
                }
            } else {
                return lastMatch;
            }
    }, string

    string = forms[0].split('').reverse().join('').replace(/./g, exec).split('').reverse().join('');
    string = leftnumber.join('') + string;

    if (forms[1] && forms[1].length) {
        leftnumber = (numbers[1] && numbers[1].length) ? numbers[1].split('').reverse() : [];
        string += '.' + forms[1].replace(/./g, exec);
    }
    return string.replace(//.$/, '');
};
alert(format(123, '#.00'))                         //输出   123.00
alert(format(123, '0.##'))                         //输出   123
alert(format(123, '0000.00 '))                     //输出   0123.00
alert(format(123456, '###,###.00'))                //输出   123,456.00
</script>
标签:
javascript,格式化

帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com