我如何使java.text.NumberFormat格式0.0d为“0”而不是“+0”? [英] How can I make java.text.NumberFormat format 0.0d as “0” and not “+0”?

查看:247
本文介绍了我如何使java.text.NumberFormat格式0.0d为“0”而不是“+0”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要结果符号,除了0.0d。即:

 
-123.45d - >-123.45,
123.45d - >+123.45,
0.0d - >0。

我在DecimalFormat的实例上调用 format.setPositivePrefix(+)以强制输入的结果中的符号。

解决方案

我确信有一个更优雅的方式,但看看是否这有效吗?

  import static org.junit.Assert.assertEquals; 

import java.text.ChoiceFormat;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;

import org.junit.Test;

public class NumberFormatTest {
@Test
public void testNumberFormat(){
NumberFormat nf = new MyNumberFormat();
assertEquals( - 1234.4,nf.format(-1234.4));
assertEquals(0.0,nf.format(0));
assertEquals(+ 0.3,nf.format(0.3));
assertEquals(+ 12.0,nf.format(12));


$ b class MyNumberFormat extends NumberFormat {
$ b $ private private DecimalFormat df = new DecimalFormat(0.0#);
private ChoiceFormat cf = new ChoiceFormat(new double [] {0.0,
ChoiceFormat.nextDouble(0.0)},new String [] {,+});
$ b @Override
public StringBuffer format(double number,StringBuffer toAppendTo,
FieldPosition pos){
return toAppendTo.append(cf.format(number))。append (df.format(数));

$ b @Override
public StringBuffer format(long number,StringBuffer toAppendTo,
FieldPosition pos){
return toAppendTo.append(cf.format( 。数))附加(df.format(数));

$ b @Override
public Number parse(String source,ParsePosition parsePosition){
throw new UnsupportedOperationException();


根据 DecimalFormat


负的子模式是可选的;如果不存在,那么以本地化减号(在大多数语言环境中为' - ')为前缀的正子模式被用作负子模式


因此新的DecimalFormat(0.0#)相当于 new DecimalFormat(0.0#; - 0.0#)

所以这会给我们: -1234.5 1234.5



现在,要将+添加到正数,我使用 ChoiceFormat
$ b


  • 0.0 <= X < ChoiceFormat.nextDouble(0.0)将使用的选择格式。 ChoiceFormat.nextDouble(0.0)是最大的数字,大于 0.0

  • ChoiceFormat.nextDouble(0.0)< = X< 1 将使用+的选择格式。




如果没有匹配,则使用第一个或最后一个索引,取决于数字(X)是太低还是太高。如果限制数组不是升序,则格式化结果将不正确。 ChoiceFormat也接受\\\∞等同于无限(INF)。

因此


  • Double.NEGATIVE_INFINITY <= X < 0 将会使用

  • 1 <= X < Double.POSITIVE_INFINITY 会使用+


Need result with sign, except for 0.0d. Ie:

 -123.45d -> "-123.45",
  123.45d -> "+123.45",
  0.0d    -> "0".

I invoke format.setPositivePrefix("+") on the instance of DecimalFormat to force the sign in the result for positive inputs.

解决方案

I'm sure there is a more elegant way, but see if this works?

import static org.junit.Assert.assertEquals;

import java.text.ChoiceFormat;
import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;

import org.junit.Test;

public class NumberFormatTest {
    @Test
    public void testNumberFormat() {
        NumberFormat nf = new MyNumberFormat();
        assertEquals("-1234.4", nf.format(-1234.4));
        assertEquals("0.0", nf.format(0));
        assertEquals("+0.3", nf.format(0.3));
        assertEquals("+12.0", nf.format(12));
    }
}

class MyNumberFormat extends NumberFormat {

    private DecimalFormat df = new DecimalFormat("0.0#");
    private ChoiceFormat cf = new ChoiceFormat(new double[] { 0.0,
            ChoiceFormat.nextDouble(0.0) }, new String[] { "", "+" });

    @Override
    public StringBuffer format(double number, StringBuffer toAppendTo,
            FieldPosition pos) {
        return toAppendTo.append(cf.format(number)).append(df.format(number));
    }

    @Override
    public StringBuffer format(long number, StringBuffer toAppendTo,
            FieldPosition pos) {
        return toAppendTo.append(cf.format(number)).append(df.format(number));
    }

    @Override
    public Number parse(String source, ParsePosition parsePosition) {
        throw new UnsupportedOperationException();
    }
}

According to DecimalFormat

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

Hence new DecimalFormat("0.0#") is equivalent to new DecimalFormat("0.0#;-0.0#")

So this would give us: -1234.5 and 1234.5

Now, to add the '+' to positve numbers, I use a ChoiceFormat

  • 0.0 <= X < ChoiceFormat.nextDouble(0.0) will use a choice format of "". ChoiceFormat.nextDouble(0.0) is the smallest number greater than 0.0.
  • ChoiceFormat.nextDouble(0.0) <= X < 1 will use a choice format of "+".

If there is no match, then either the first or last index is used, depending on whether the number (X) is too low or too high. If the limit array is not in ascending order, the results of formatting will be incorrect. ChoiceFormat also accepts \u221E as equivalent to infinity(INF).

Hence

  • Double.NEGATIVE_INFINITY <= X < 0 will use "".
  • 1 <= X < Double.POSITIVE_INFINITY will use "+".

这篇关于我如何使java.text.NumberFormat格式0.0d为“0”而不是“+0”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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