简体   繁体   English

四舍五入时,DecimalFormat是否不考虑十进制值?

[英]Is DecimalFormat not considering the decimal values when round it?

Am trying to format the amount.But the decimal values not being rounded in correct manner. 我正在尝试格式化金额,但是十进制值未以正确的方式四舍五入。

public class Testing {    
    private java.text.DecimalFormat dispAmt;    
    public Testing() {                
    }    
    public static void main(String args[]){   
       Testing testing=new Testing();    
       testing.dispAmt = new java.text.DecimalFormat("##,##,##0.00");   
       // Line #8
       System.out.println(testing.dispAmt.format(1974.545));     
       System.out.println(testing.dispAmt.format(1974.535));   
    }   
 }    

OutPut:
=========
1,974.54    
1,974.54


In above prog what's wrong with 8th line. 在上面的编中,第8行有什么问题。 why it is not rounding as "1,974.55"?? 为什么不将其舍入为“ 1,974.55”? where am doing mistake!! 在哪里做错! pls suggest.. 请建议..

By default DecimalFormat uses rounding mode of HALF_EVEN which 默认情况下, DecimalFormat使用的舍入模式HALF_EVEN 其中

behaves as for RoundingMode.HALF_UP if the digit to the left of the discarded fraction is odd; 如果舍弃的分数左侧的数字为奇数,则其行为与RoundingMode.HALF_UP相同。 behaves as for RoundingMode.HALF_DOWN if it's even. 行为与RoundingMode.HALF_DOWN相同。

In this case the second last least significant digit of 4 of 1974.545 is even so the value is rounded down. 在这种情况下, 1974.545第二个最低有效位 4是偶数,因此该值将四舍五入。 Conversely, the value of 3 is odd so rounding up occurred there in the subsequent format statement. 相反,值3为奇数,因此在随后的format语句中四舍五入。

Try using RoundingMode.HALF_UP . 尝试使用RoundingMode.HALF_UP

testing.dispAmt.setRoundingMode(RoundingMode.HALF_UP);

DecimalFormat#format is not rounding! DecimalFormat#format不舍入! it is just cutting off the values you don't display by default. 它只是切断默认情况下不显示的值。

DecimalFormat uses half-even rounding (see ROUND_HALF_EVEN) for formatting. DecimalFormat使用半数舍入(请参阅ROUND_HALF_EVEN)进行格式化。

http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html

you have to set the RoundingMode manually 您必须手动设置RoundingMode

dispAmt.setRoundingMode(RoundingMode.HALF_UP);

You need to set the RoundingMode for the formatter by setting it using the setRoundingMode() method, as by default, it does not round the value. 您需要通过使用setRoundingMode()方法对其进行设置来设置格式化程序的RoundingMode ,因为默认情况下,它不会舍入该值。

dispAmt.setRoundingMode(RoundingMode.CEILING); // Ex 1
dispAmt.setRoundingMode(RoundingMode.HALF_UP); // Ex 2 but I guess you need this in your case

There are many RoundingMode s available. 有很多RoundingMode可用。 Do check the docs for that. 请为此检查文档。

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. DecimalFormat是NumberFormat的一个具体子类,用于格式化十进制数字。 And this doesn't round up the given numbers. 这并没有舍入给定的数字。 INFO This just use the pattern you are given to display the number. 信息这仅使用您提供的模式显示数字。

DecimalFormat provides rounding modes defined in RoundingMode for formatting. DecimalFormat提供在RoundingMode中定义的入模式以进行格式化。 By default, it uses RoundingMode.HALF_EVEN. 默认情况下,它使用RoundingMode.HALF_EVEN。

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

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