0

I can't figure out why the numbers being retuned by this function are off from what they should be. For the data that is entered and multiplied by 0.158 the wrong values are being returned by the calculations. For example 242 * .185 should return 44.77 but instead it is returning 38.236. I really can't figure out where I have gone wrong with this.

import java.util.*;

public class converter {

    public static void main(String[] args) {
        String s = "242,948,275,879,304";
        List<String> list = Arrays.asList(s.split(","));
        String end = "";
        Scanner reader = new Scanner(s);
        for (int i = 0; i < list.size(); i++)
        {
            String str = list.get(i);
            double j = Float.parseFloat(str);
            double number = 0.158;
            j = j * number;
            System.out.println(j);
            int k = (int) j;
            end += k +",";
        }
        System.out.println(end);
    }

}
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
  • 8
    `0.158` != `0.185`. – tkausl Feb 21 '19 at 05:25
  • typo mistake, all answers are ok, 242 * 0.158 = 38.236. – mitesh7172 Feb 21 '19 at 05:34
  • 1
    In addition to the probable typo, please be aware that floating point math has its limitations: see [Is floating point math broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – KevinO Feb 21 '19 at 05:37
  • Oh wow thank you so much for noticing that! I swear I looked at this for way too long without seeing that. I really appreciate you guys taking the time to look at this. Thanks! – Eli Gould Feb 21 '19 at 07:59

0 Answers0