double intelligent division
Hi, I have a double, i.e 10.0 (of course I can create some Double)
I want to divide it in 3 parts.. with a normal division I'll have
3.333333
3.333333
3.333333
I have too many decimals and I don't want a periodic number..
I want something like:
3.33 (2 decimals)
3.33
3.34
any hints?
Yes. Don't use a double, but an int or a long, and use the % operator to get the remainder of the division. You can add that to the last part.
the problem is that I can not use an int or double because the nuber that has to be divided, can be something like 10.5, I mean, it is not allways an integer..
> the problem is that I can not use an int or double> because the nuber that has to be divided, can be> something like 10.5, I mean, it is not allways an> integer..So how are you going to break up 10.5?
10,5 = 3.5 + 3.5 + 3.5
the problem is when I have a periodic number because i need as aoutput nubers with just 2 decimal digits.. if I have 10.0 I have
3.333333
3.333333
3.333333
I need to get the fitst number (3.333333) and cut it like 3.33 (2 decimal digits, and I don't know haw to do it) then I will have
3.33
3.33
last one = 10.0 - (3.33 * 2) = 3.34
the main problem at this point, is to know how to "cut" the decimal digits after the second one y.xx (and not y.xxxxxxxx)
Can you ever have an input number with more than 2 decimal digits? If you can, then your problem cannot be solved in the general case. If you can't then you can use ints or some other method of doing fixed point arithmetic.
Experiment with the java.math.BigDecimal class.
Cutting: DecimalFormatBut I still don't see your algorithm. When do you cut? What in case of 10.65?
This might help in formatting to up to two decimal places
import java.text.DecimalFormat;
// rest of code...
DecimalFormat df = new DecimalFormat("0.##");
formattednum = df.format(anum);
formattednum = String
anum = double
For your last number, I guess convert all formatted number results to doubles via Double.parseDouble, add them all together, then subtract from the original number.
Something like this?
import java.text.DecimalFormat;
public class Test {
public static void main(String args[]) {
chopItUp(10.0);
}
public static void chopItUp(double num) {
final int numsAfterComma = 2;
double part_1 = num/3.0;
double part_2, part_3;
part_1 = (double)((int)(part_1 * Math.pow(10, numsAfterComma)) /
Math.pow(10, numsAfterComma));
part_2 = part_1;
part_3 = num - (part_1 + part_2);
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(num+" = "+df.format(part_1)+
" + "+df.format(part_2)+
" + "+df.format(part_3));
}
}
BigDecimal val = new BigDecimal(10.0);
val = val.setScale(2);
BigDecimal oneThird = val.divide(new BigDecimal(3.0), BigDecimal.ROUND_HALF_UP);
System.out.println(val + "/3 = " + oneThird);
val = val.subtract(oneThird);
System.out.println("after subtracting one-third, value is now:" + val);
val = val.subtract(oneThird);
System.out.println("after subtracting another one-third, value is now:" + val);
10.00/3 = 3.33
after subtracting one-third, value is now:6.67
after subtracting another one-third, value is now:3.34
thanx to everyone
I was also thinking about to multiply it for 100 (333.3333), then cast to int (333), then cast to double and divide it by 100..(3.33) :D
anyway, in the solution of prometheuzz, i shoudl add
part_3 = (double)((int)(part_3 * Math.pow(10, numsAfterComma)) /
Math.pow(10, numsAfterComma));
(for an example, try the code with 10.65)
thanx it works, really thanx because I've learned a lot!
I guess the BigDecimal solution is invisible then, or are you "not allowed" to use that class?
> I guess the BigDecimal solution is invisible then, or> are you "not allowed" to use that class?What BigDecimal solution?; )
Hmm, for some values, like 10.65 my solution ends up throwing an ArithmeticException.
Change this line:
> val = val.setScale(2);
to this:
> val = val.setScale(2, BigDecimal.ROUND_HALF_UP);
That is, if that solution isn't still invisible. And this post isn't either ;-)
Just a hint - for a financial applications always use BigDecimal. It uses (value * 10^scale) format for internal representation while double uses (manitsa * 2^exponent) format. This is tiny but important difference. For example the 1/3 - it is a periodic number in both decimal and binary notation but, for your surprise 1/10 is periodic in binary notation but not periodic in decimal notation! So, if you really insist on accurate arithmetics or when you need to count money use BigDecimal. Otherwise you are asking for troubles.