简体   繁体   English

使用DecimalFormat删除最后三个零

[英]Removing last three zeros with DecimalFormat

I'm using jfreechart to display axis values in milliseconds but I'd like to show the axis labels in seconds, so I'd use domainAxis.setNumberFormatOverride(new DecimalFormat("???")); 我正在使用jfreechart以毫秒为单位显示轴值,但是我想以秒为单位显示轴标签,所以我将使用domainAxis.setNumberFormatOverride(new DecimalFormat("???"));

So what's the syntax to remove last three zeros? 那么删除最后三个零的语法是什么? eg: 1.000, 2.000, 3.000 to 1, 2, 3. 例如:1.000、2.000、3.000至1、2、3。

Thanks to this other post I managed to divide by 1000 subclassing NumberFormat 由于这篇文章,我成功地将1000子类划分为NumberFormat

So final code is simply: 所以最终的代码很简单:

private static final NumberFormat THOUSANDS = new NumberFormat() {

    @Override
    public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {

        new DecimalFormat().format(number / 1000D, toAppendTo, pos);

        return toAppendTo;
    }

    @Override
    public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
        return format((double) number, toAppendTo, pos);
    }

    @Override
    public Number parse(String source, ParsePosition parsePosition) {
        return null;
    }
};

And the call: 并致电:

domainAxis.setNumberFormatOverride(THOUSANDS);

Try this 尝试这个

String label= "1.000"
String slabel= new DecimalFormat("0.####").format(Double.parseDouble(label));
System.out.println(slabel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM