具有等价的负面和正面模式的Java NumberFormat [英] Java NumberFormat with equivalent negative and positive pattern

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

问题描述



NumberFormat的规范指出,您可以指定一个数字,而不是任何前缀(或相同的前缀)每个子模式都有一个前缀,一个数字部分和一个数字部分,而另一个子模式则用一个分号分隔。后缀。负
子模式是可选的;如果不存在,则使用
作为负子模式,正数子模式
前缀为本地化减号(在大多数语言环境中为 - )。也就是说,单是0.00相当于
0.00; -0.00。

只要正面和负面的子图案具有不同的前缀或后缀。如果它们相同,则默认为带有减号的默认行为。



我想要的格式是:


  • +1 as:01

  • -1 as:01


我知道我可以使用Math.abs(),这在JSP / EL环境中并不那么容易,而且这是一个纯粹的格式问题,所以应该在JSP中完成,但这根本就不重要。
我找不到有关该行为的文档中的任何提示。当我读到上面的内容时,我认为只要我指定了任何负面的子模式,它就会被使用。事实并非如此。



有人知道这件事吗?我可能会提出一个Oracle的错误,但首先要检查,如果我失去了一些东西。

解决方案

假设你有JSTL在您的wepapp中,您可以使用以下内容:

 <%@ taglib prefix =fmturi =http:/ /java.sun.com/jsp/jstl/fmt\"%> 
<%@ taglib prefix =fnuri =http://java.sun.com/jsp/jstl/functions%>

希望这个帮助...


I want to format a number without any prefix (or the same prefix) for both positive and negative numbers.

The specification of NumberFormat states that you can specify a sub pattern for positive numbers and a sub pattern for negative numbers, separated by a semicolon.

Each subpattern has a prefix, numeric part, and suffix. The negative subpattern is optional; if absent, then the positive subpattern prefixed with the localized minus sign ('-' in most locales) is used as the negative subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00".

This works, as long as the positive and negative sub patterns have distinct prefixes or suffixes. If they are the same, it defaults to the default behavior with a minus sign.

What I want is to format:

  • +1 as :01
  • -1 as :01

I know that I can use Math.abs(), which is not so easy in a JSP/EL environment and it's a pure formatting issue, so should be done in JSP, but that's not the point at all. I couldn't find any hint in the documentation about that behavior. When I read the above, I think that as soon as I specify any negative sub pattern, it will be used. Which is not the case.

Does anyone know something about this? I might file a bug with Oracle, but wanted to check first, if I'm missing something.

解决方案

Assuming you have JSTL in your wepapp, you could use the following:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<fmt:formatNumber pattern=":#" minIntegerDigits="2" value="${fn:replace('-1', '-', '')}" />

Hope this help...

这篇关于具有等价的负面和正面模式的Java NumberFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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