P
P
P_Alexander2017-01-20 18:40:46
Java
P_Alexander, 2017-01-20 18:40:46

The problem with BigDecimal, with the miltiply() method, what's the matter?

The problem is that I don't understand why this line return h.multiply(y); doesn't think it's right!! should return 4, but returns 8!
Here is the
package testone code;
import java.math.*;
import java.util.Scanner;
public class TestOne
{
BigDecimal s;
Big Decimal f;
Big Decimal h;
BigDecimal y;
public BigDecimal ceil (int a, int b)
{
s = BigDecimal.valueOf(a);
h = BigDecimal.valueOf(b);
y = BigDecimal.valueOf(1);
//return((a+b-1)/b);
f = ((s.add(h).subtract(y)).divide(h,BigDecimal.ROUND_HALF_DOWN));
System.out.println(" ceil ---" + f);
return f;
}
public BigDecimal getRezult(int m, int n, int a)
{
h = ceil(m , a);
System.out.println("hhhhh ---" + h);
y = BigDecimal.valueOf(0);
y = ceil(n, a);
System.out.println("yyyyyyyyyyyy ---" + y);
System.out.println("h.multiply(y) -----" + h.multiply(y));
return h.multiply(y);
//return ceil(m , a) * ceil(n , a);
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int a = scan.nextInt();
TestOne z = new TestOne();
System.out.println(z.getRezult(m, n, a));
}
}
Program output:
run:
6
6
4
ceil ---2
hhhhhh ---2
ceil ---2
yyyyyyyyyyyy ---2
h.multiply(y) -----8
8
BUILD SUCCESSFUL (total time: 10 seconds)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Oparin, 2017-01-20
@P_Alexander

When calling y = ceil(n , a); you change the value of h

S
Stanislav Zemlyakov, 2017-01-20
@redtom

I beg your pardon, but when you ask your question and post the code, at least worry about its readability, because people like me may want to help, but they don’t delve into solid text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question