Java DecimalFormat科学记数法问题 [英] Java DecimalFormat Scientific Notation Question

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

问题描述

我使用Java的 DecimalFormat 类以科学记数法打印出数字。但是,我有一个问题。无论价值如何,我都需要固定长度的字符串,并且十个幂的符号正在把它扔掉。目前,这是我的格式:
$ b

  DecimalFormat format = new DecimalFormat(0.0E0); 

这给了我以下组合:1.0E1,1.0E-1,-1.0E1和 - 1.0E-1。

我可以使用 setPositivePrefix 获得:+ 1.0E1,+ 1.0E-1,-1.0E1和 - 1.0E-1或者其他我喜欢的东西,但是不影响标志的力量!

有什么办法可以做到这一点长度字符串?谢谢!

编辑:啊,所以没有办法用Java的现有的DecimalFormat API来完成。感谢您的建议!我想我可能必须继承,因为我受限于已经存在的接口。 这是一种方法。 Hokey,也许,但它的工作原理... $ / b>

  public class DecimalFormatTest extends TestCase {
private static class MyFormat extends NumberFormat {
私有的最终DecimalFormat小数;
$ b $ public MyFormat(String pattern){
decimal = new DecimalFormat(pattern);

$ b $ public StringBuffer format(double number,StringBuffer toAppendTo,FieldPosition pos){
StringBuffer sb = new StringBuffer();
sb.append(modified(Math.abs(number)> 1.0,decimal.format(number,toAppendTo,pos).toString()));
return sb;
}

私有字符串修改(布尔大,字符串s){
返回大? s.replace(E,E +):s;


public StringBuffer format(long number,StringBuffer toAppendTo,FieldPosition pos){
StringBuffer sb = new StringBuffer();
sb.append(modified(true,decimal.format(number,toAppendTo,pos).toString()));
return sb;


public Number parse(String source,ParsePosition parsePosition){
return decimal.parse(source,parsePosition);
}

public void setPositivePrefix(String newValue){
decimal.setPositivePrefix(newValue);
}
}
私人MyFormat格式;

保护无效setUp()抛出异常{
format = new MyFormat(0.0E0);
format.setPositivePrefix(+);
}

public void testPositiveLargeNumber()抛出异常{
assertEquals(+ 1.0E + 2,format.format(100.0));

$ b $ public void testPositiveSmallNumber()throws Exception {
assertEquals(+ 1.0E-2,format.format(0.01));

$ b $ public void testNegativeLargeNumber()抛出异常{
assertEquals( - 1.0E + 2,format.format(-100.0));
}
$ b $ public void testNegativeSmallNumber()throws Exception {
assertEquals( - 1.0E-2,format.format(-0.01));






$ b

另外,你可以使用子类 DecimalFormat,但我觉得它通常更清洁,不要从具体类的子类。


I'm using Java's DecimalFormat class to print out numbers in Scientific Notation. However, there is one problem that I have. I need the strings to be of fixed length regardless of the value, and the sign on the power of ten is throwing it off. Currently, this is what my format looks like:

DecimalFormat format = new DecimalFormat("0.0E0");

This gives me the following combinations: 1.0E1, 1.0E-1, -1.0E1, and -1.0E-1.

I can use setPositivePrefix to get: +1.0E1, +1.0E-1, -1.0E1, and -1.0E-1, or whatever I like, but it doesn't affect the sign of the power!

Is there any way to do this so that I can have fixed length strings? Thanks!

Edit: Ah, so there's no way to do it using Java's existing DecimalFormat API? Thanks for the suggestions! I think I may have to subclass DecimalFormat because I am limited by the interface that is already in place.

解决方案

Here's one way. Hokey, perhaps, but it works...

public class DecimalFormatTest extends TestCase {
    private static class MyFormat extends NumberFormat {
        private final DecimalFormat decimal;

        public MyFormat(String pattern) {
            decimal = new DecimalFormat(pattern);
        }

        public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
            StringBuffer sb = new StringBuffer();
            sb.append(modified(Math.abs(number) > 1.0, decimal.format(number, toAppendTo, pos).toString()));
            return sb;
        }

        private String modified(boolean large, String s) {
            return large ? s.replace("E", "E+") : s;
        }

        public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
            StringBuffer sb = new StringBuffer();
            sb.append(modified(true, decimal.format(number, toAppendTo, pos).toString()));
            return sb;
        }

        public Number parse(String source, ParsePosition parsePosition) {
            return decimal.parse(source, parsePosition);
        }

        public void setPositivePrefix(String newValue) {
            decimal.setPositivePrefix(newValue);
        }
    }
    private MyFormat    format;

    protected void setUp() throws Exception {
        format = new MyFormat("0.0E0");
        format.setPositivePrefix("+");
    }

    public void testPositiveLargeNumber() throws Exception {
        assertEquals("+1.0E+2", format.format(100.0));
    }

    public void testPositiveSmallNumber() throws Exception {
        assertEquals("+1.0E-2", format.format(0.01));
    }

    public void testNegativeLargeNumber() throws Exception {
        assertEquals("-1.0E+2", format.format(-100.0));
    }

    public void testNegativeSmallNumber() throws Exception {
        assertEquals("-1.0E-2", format.format(-0.01));
    }
}

Alternatively you could subclass DecimalFormat, but I find it generally cleaner not to subclass from concrete classes.

这篇关于Java DecimalFormat科学记数法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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