Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

javascript - JQuery数字格式(JQuery Number Formatting)

There are way too many questions and answers about this basic functionality, I cannot see the wood for the trees.

(关于这个基本功能有太多的问题和答案,我看不到树木的木材。)

In Java there is only one simple answer ( java.text.NumberFormat and its subclasses) so I'm sure the majority of plugins, questions and answers will mature eventually to a de-facto standard for JQuery.

(在Java中只有一个简单的答案( java.text.NumberFormat及其子类),所以我确信大多数插件,问题和答案最终都会成熟为JQuery的事实标准。)

This plugin is the best I found so far, but I don't know if it's still developed, is mature etc.

(这个插件是我迄今为止发现的最好的插件 ,但我不知道它是否仍在开发中,是否成熟等。)

http://plugins.jquery.com/project/numberformatter

(http://plugins.jquery.com/project/numberformatter)

Is there a better solution?

(有更好的解决方案吗?)

Is it mature / active enough to rely on?

(它是否足够成熟/活跃?)


Edit:

(编辑:)

I would like to be able to format currencies, decimal, integers based on the same format patterns Java uses, so that data recieved on the client side can be formatted without sending it first to the server.

(我希望能够基于Java使用的相同格式模式格式化货币,十进制,整数,以便可以格式化客户端收到的数据,而无需先将其发送到服务器。)

eg

(例如)

Format 1000 to $1,000 or 1,000.00 etc (locale support is nice)

(格式1000$1,0001,000.00等(区域设置支持很好))

Seems that http://plugins.jquery.com/project/numberformatter does the job but the question was: "Am I using the right thing?"

(似乎http://plugins.jquery.com/project/numberformatter完成了这项工作,但问题是:“我使用的是正确的吗?”)

or "Is there a better way to do so?"

(或者“有更好的方法吗?”)

  ask by Eran Medan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would recommend looking at this article on how to use javascript to handle basic formatting:

(我建议看看这篇关于如何使用javascript来处理基本格式的文章:)

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(d+)(d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

source: http://www.mredkj.com/javascript/numberFormat.html

(来源: http//www.mredkj.com/javascript/numberFormat.html)

While jQuery can make your life easier in a million different ways I would say it's overkill for this.

(虽然jQuery可以通过百万种不同的方式让你的生活更轻松,但我认为这样做太过分了。)

Keep in mind that jQuery can be fairly large and your user's browser needs to download it when you use it on a page.

(请记住,jQuery可能相当大,当您在页面上使用它时,用户的浏览器需要下载它。)

When ever using jQuery you should step back and ask if it contributes enough to justify the extra overhead of downloading the library.

(当你使用jQuery时,你应该退后一步,询问它是否足以证明下载库的额外开销。)

If you need some sort of advanced formatting (like the localization stuff in the plugin you linked), or you are already including jQuery it might be worth looking at a jQuery plugin.

(如果您需要某种高级格式(如您链接的插件中的本地化内容),或者您已经包含jQuery,那么可能值得查看jQuery插件。)

Side note - check this out if you want to get a chuckle about the over use of jQuery.

(旁注 - 如果你想对过度使用jQuery轻笑一下, 请检查一下。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...