java - What types to use for my numbers when calculating -
java - What types to use for my numbers when calculating -
so need calculate value.
the input this:
a seed/m2. value might illustration 56 might 56.7 also. b in g's. instance 600g c % value, might 90.6 d % value, might 90.6
the result should kg/ha
regular int not cutting it. value of (56 * 600 / 100 / 100) / 100
0.0336
. multiply 10000 lose precision.
i tried bigdecimal gave me arithmeticexception: “non-terminating decimal expansion; no exact representable decimal result”
when changed values of % variables else 100.
what best alternative go this? calculation easy in exel knew how convert each value automatically, doing in java code thing.
my solutions:
int version:
int = integer.decode(germinativeseed.gettext().tostring()); int b = integer.decode(seedmass.gettext().tostring()); int c = integer.decode(clean.gettext().tostring()); int d = integer.decode(germinative.gettext().tostring()); int result2 = ( * b / c / d) / 100;
result 0
bigdecimal solution:
bigdecimal result2; bigdecimal = new bigdecimal(germinativeseed.gettext().tostring()); bigdecimal b = new bigdecimal(seedmass.gettext().tostring()); bigdecimal c; bigdecimal d; if (clean.gettext().tostring().equals("")) { c = new bigdecimal("100"); } else { c = new bigdecimal(clean.gettext().tostring()); } if (germinative.gettext().tostring().equals("")) { d = new bigdecimal("100"); } else { d = new bigdecimal(germinative.gettext().tostring()); } bigdecimal hundred = new bigdecimal("100"); bigdecimal test = new bigdecimal("10000"); result2 = a.multiply(b); result2 = result2.divide(c, 2, roundingmode.half_up); result2 = result2.divide(d, 2, roundingmode.half_up); result2 = result2.divide(hundred, 2, roundingmode.half_up); result2 = result2.multiply(test);
result right if % values 100%.
double seed = (double) seedinput; double m2 = (double) m2input; double b = (double) binput; // unit 'g' not relevant double c = (double) cinput; double d = (double) dinput; double = seed / m2; int result2 = ( * b / c / d) / 100.0;
so converted double won't have problems implicit conversions int.
java
Comments
Post a Comment