格式设定 [英] Formatting

查看:65
本文介绍了格式设定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在javascript中格式化字符串,货币,日期时间和数字?

How to format the string, currency, datetime and Number in javascript?

推荐答案

谢谢您的提问.您可以按照下面的代码.

Thank you for your question. You can follow the bellow code.

function CommaFormatted(amount)
{
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.',2)
    var d = a[1];
    var i = parseInt(a[0]);
    if(isNaN(i)) { return ''; }
    var minus = '';
    if(i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while(n.length > 3)
    {
        var nn = n.substr(n.length-3);
        a.unshift(nn);
        n = n.substr(0,n.length-3);
    }
    if(n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if(d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}



日期格式



Date Format

var dt = new Date();
var dtstring = dt.getFullYear()
    + '-' + pad2(dt.getMonth()+1)
    + '-' + pad2(dt.getDate())
    + ' ' + pad2(dt.getHours())
    + ':' + pad2(dt.getMinutes())
    + ':' + pad2(dt.getSeconds());



参考: http://www.web-source.net/web_development/currency_formatting.htm [ ^ ]

谢谢,
Mamun



Ref:http://www.web-source.net/web_development/currency_formatting.htm[^]

Thanks,
Mamun


使用Microsoft Ajax工具包实现您的目标.
在javascript中称为MASK.
Ajax工具箱中有一个控件,您可以在其中使用HtmlControls中的扩展控件,例如输入html控件.
Use Microsoft Ajax toolkit to achieve your goal.
in javascript it is called MASK.
There is a control in Ajax toolkit in which you can use Extended controls in HtmlControls like input html control.


这篇关于格式设定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆