Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 分贝格式不正确_Java_Decimalformat_Ceil - Fatal编程技术网

Java 分贝格式不正确

Java 分贝格式不正确,java,decimalformat,ceil,Java,Decimalformat,Ceil,我们目前正在进行一些测试,涉及小数点后两位的上限数字。为了实现这一点,我们使用Java的 然而,这些测试得到了奇怪的结果,特别是当我们希望获得一个'0.00xxx'数字时 以下是测试使用的DecimalFormatter的实例: DecimalFormat decimalFormatter = new DecimalFormat("#########0.00"); decimalFormatter.setRoundingMode(RoundingMode.CEILING); 此测试按预

我们目前正在进行一些测试,涉及小数点后两位的上限数字。为了实现这一点,我们使用Java的

然而,这些测试得到了奇怪的结果,特别是当我们希望获得一个'0.00xxx'数字时

以下是测试使用的DecimalFormatter的实例:

DecimalFormat decimalFormatter = new DecimalFormat("#########0.00");
    decimalFormatter.setRoundingMode(RoundingMode.CEILING);
此测试按预期工作,即正确接收:

//The below asserts as expected
    @Test
    public void testDecimalFormatterRoundingDownOneDecimalPlace3()
    {
        String formatted = decimalFormatter.format(234.6000000000001);
        Assert.assertEquals("Unexpected formatted number", "234.61", formatted);
    }
然而,这并不是:

//junit.framework.ComparisonFailure: Unexpected formatted number 
//Expected :0.01
//Actual   :0.00

    @Test
    public void testSmallNumber()
    {
        double amount = 0.0000001;

        String formatted = decimalFormatter.format(amount);
        Assert.assertEquals("Unexpected formatted number", "0.01", formatted);
    }
你能解释一下为什么我们会有这种行为吗。谢谢

编辑:根据注释要求的另一个测试。还是不行

//junit.framework.ComparisonFailure: null
//Expected :0.01
//Actual :0.00

@Test
public void testStackOverflow() throws Exception
{
double amount = 0.0000006;
String formatted = decimalFormatter.format(amount);
Assert.assertEquals("Unexpected formatted number", "0.01", formatted);
}

我注意到,要使其工作,大于0的数字必须在模式的范围内。这是一个bug还是我遗漏了什么?

看起来像个bug。此代码

    BigDecimal bd = new BigDecimal("0.0000001");
    bd = bd.setScale(2, RoundingMode.CEILING);
    System.out.println(bd);
产生正确的结果

0.01

+我的答案是错的:(与0.0000006一样。结果是因为舍入只考虑一个数字超出请求的格式。数字被截断到<代码> 0 <代码>,然后舍入到<代码> 0 /代码>。但是我不确定如何改变这种行为。@ Bobby,但是第一个测试没有考虑超出请求格式的数字。最后一个1和天花板…=呼啦圈,然后我就出局了。。。