有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从35418.8中获得35400?

我正在做以下工作:

SELECT round(35418.8, -2) FROM compa_8039 

结果是:35400

在java中,我必须得到相同的结果,但我不能

在java中,当值等于35418.8时,如何实现这一点


共 (2) 个答案

  1. # 1 楼答案

    使用BigDecimal类及其^{}方法也可以实现同样的效果:

    BigDecimal bd = BigDecimal.valueOf(35418.8);
    bd = bd.setScale(-2, BigDecimal.ROUND_FLOOR);
    int result = bd.intValue();
    
    System.out.println(result);
    

    这将打印值:

    35400
    
  2. # 2 楼答案

    ROUND(X), ROUND(X,D)

    Rounds the argument X to D decimal places. The rounding algorithm depends on the data type > of X. D defaults to 0 if not specified. D can be negative to cause D digits left of the decimal point of the value X to become zero.

    看到了吗

    http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_round

    小数点左边两位的35418.8是35400

    编辑:

    很抱歉在java中这样做

    public class HelloWorld{
    
         public static void main(String []args){
           Double x=35418.18;
            int representation=(x.intValue()/100)*100;
            System.out.println(representation);
         }
    }