JQuery数字格式 [英] JQuery Number Formatting

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

问题描述

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

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

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

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.

这个插件是我发现的最好的到目前为止,但我不知道它是否仍然开发,是否成熟等。

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

有更好的解决方案吗?它是否足够成熟/活跃?

Is there a better solution? Is it mature / active enough to rely on?

编辑:

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

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.

例如。

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

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

似乎 http://plugins.jquery.com/project/numberformatter 完成这项工作,但问题是:我使用的是正确的吗?或者有更好的方法吗?

Seems that http://plugins.jquery.com/project/numberformatter does the job but the question was: "Am I using the right thing?" or "Is there a better way to do so?"

推荐答案

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

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

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;
}

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

虽然jQuery可以让你的生活更轻松一百万种不同的方式我认为这样做太过分了。请记住,jQuery可能相当大,当您在页面上使用它时,用户的浏览器需要下载它。

While jQuery can make your life easier in a million different ways I would say it's overkill for this. 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,那么可能值得查看一个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。

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

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

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