Handling numbers with lot of values after decimal in Java
I do
Collections.frequency(List<String>, List<String>.get(i)) / List<String>.size()
The output for the above calculation s开发者_C百科hould be (for example)
0.00631
0.0002378
0.00571
but I get 0.0, instead.
How do I handle this? I keep getting 0.0 with double and float
Thanks
If the values 0.00631, 0.0002378 and 0.00571 are expected results from divisions, make sure you're not doing integer divisions. That is, make sure to cast numerator or denominator to float or double.
Instead of
double fraction = someInt / someOtherInt;
you can do
double fraction = (double) someInt / someOtherInt;
In your particular case, you could try something like
(double) Collections.frequency(list, list.get(i)) / list.size();
Use BigDecimal that does not introduce any rounding for very big or very small numbers at the cost of bigger memory consumption and slower computations. This class is a must for any financial data.
BigDecimal val = new BigDecimal("10000000000000.0002378");
System.out.println(val);
加载中,请稍侯......
精彩评论