How can I make java.text.NumberFormat format 0.0d as “0” and not “+0”?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 00:26:43

问题


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.


回答1:


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 "+".



回答2:


Thanks a lot guys, some very good ideas here.

Based on what has been suggested, I decided to use two formats: zeroFormat for special-casing for 0.0d, and nonZeroFormat for the rest of the cases. I am hiding the implementation behind an IDisplayableValueFormatter (that is used by a custom UI control) and don't need to adhere to the NumberFormat contract/interface.




回答3:


Java has both a "negative zero" and a "positive zero". They have different representations, but compare as being equal to each other.

If you must have a plus sign preceding your positive values, but you don't want it for positive zero, then you may need to do something like this to temporarily clear the prefix:

try {
    if (val == 0.0) {
        format.setPositivePrefix("");
    }
    result = format.format(val);
}
finally {
    format.setPositivePrefix("+");
}



回答4:


I just ran into this on a project, and I didn't find any of the answers here particularly satisfactory.

What I was able to do instead (which may or may not work for you) is:

// Convert negative zero to positive zero.  Even though this
// looks like it should be a no-op, it isn't.
double correctedValue = (value == 0.0 ? 0.0 : value);

Then the value will be correctly formatted by the DecimalFormat class.

I'm actually a bit surprised that Java doesn't have a similar construct built-in.



来源:https://stackoverflow.com/questions/673841/how-can-i-make-java-text-numberformat-format-0-0d-as-0-and-not-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!