简体   繁体   English

方法中的Java小数点

[英]Java decimal point in method

I am little bit lost with double decimal point at the moment. 我现在有点小数点丢失了。

I have basically two methods, which will set the values for double amount and double receive. 我基本上有两种方法,它们将设置double amount和double receive的值。 Then another integer variable where I would like to set the (receive - amount) * 100. 然后另一个整数变量,我想设置(接收 - 金额)* 100。

For example if I have two double values and I want to set their difference to an int value, then would it be possible? 例如,如果我有两个double值,并且我想将它们的差值设置为int值,那么它是否可能?

My problem is that if I try to find the difference between two values, then eg (10.0- 9.40), then it will be 0.599999999. 我的问题是,如果我试图找到两个值之间的差异,那么例如(10.0- 9.40),那么它将是0.599999999。 How can I get 0.60 out of it inside the method and use it? 如何从方法中获取0.60并使用它? I know how to use NumberFormat or DecimalFormat. 我知道如何使用NumberFormat或DecimalFormat。 Should I use one inside the method to set the number of decimal points? 我应该在方法中使用一个来设置小数点吗?

you can round off the value im using a decimalformat to round off the number. 你可以使用小数格式来舍入值im来舍入数字。 You can pass a double variable inside the method and this will return a number rounded off to 2 decimal points. 您可以在方法内传递一个double变量,这将返回一个舍入为2个小数点的数字。

double RoundTo2Decimals(double val) {
    DecimalFormat df2 = new DecimalFormat("###.##");
    return Double.valueOf(df2.format(val));
}

You can use BigDecimal to perform the rounding, or you can use maths like this. 您可以使用BigDecimal执行舍入,或者您可以使用这样的数学。 It basically multiplies by 100, rounds and divides by 100. 它基本上乘以100,轮次和除以100。

/**
 * Performs a round which is accurate to within 1 ulp. i.e. for values very close to 0.5 it
 * might be rounded up or down. This is a pragmatic choice for performance reasons as it is
 * assumed you are not working on the edge of the precision of double.
 *
 * @param d value to round
 * @return rounded value
 */
public static double round2(double d) {
    final double factor = 1e2;
    return d > WHOLE_NUMBER / factor || d < -WHOLE_NUMBER / factor ? d :
            (long) (d < 0 ? d * factor - 0.5 : d * factor + 0.5) / factor;
}

Easiest solution could be below. 最简单的解决方案可能在下面。 Modifications and improvements are welcomed. 欢迎修改和改进。

       double x  =10.0;
       double y  =9.40;
       int xy =0;

       DecimalFormat df = new DecimalFormat("#.##");      

       xy = (int) (Double.valueOf(df.format(x-y))*100);
       System.out.println(xy);

I think I figured it out by using Math.round(). 我想我通过使用Math.round()来解决它。

I will just ask whether my solution is a good or a bad idea to use? 我只想问一下我的解决方案是好还是坏? I am not just so familiar with BigDecimal. 我不仅仅熟悉BigDecimal。 Long story short about the code. 长话短说关于代码。 Example inputs are as: a = 9.40 and b = 10.0 示例输入为:a = 9.40,b = 10.0

private int difference;
private double amountDue;
private double receive;

public void setAmount(double a) {
    amountDue = a;
}

public void receive(double b) {
    receive = b;
    difference = (int)Math.round(100 * (receive - amount));

I just needed to get int difference as 0.60 * 100 = 60, but as I mentioned before then just calculating the difference caused 0.59999999. 我只需要得到int差异为0.60 * 100 = 60,但正如我之前提到的那样只计算差异导致0.59999999。

Just an extra question. 只是一个额外的问题。 Is it ok for me to initialize int balance variable inside one method as I have done? 我可以在一个方法中初始化int balance变量吗?

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

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