Convert a string into an array or list of floats
arrays, java, list, string
Solution
With rounding to 3 decimals...
String[] parts = input.split("#");
float[] numbers = new float[parts.length];
for (int i = 0; i < parts.length; ++i) {
float number = Float.parseFloat(parts[i]);
float rounded = (int) Math.round(number * 1000) / 1000f;
numbers[i] = rounded;
}
Problem
I have a string of fourteen values seperated by # 0.1#5.338747#0.0#.... and so on I want to convert each value from a string to a float or double to 3 decimal places. I can do most of this the long way... ``` str = "0.1#0.2#0.3#0.4"; String[] results; results = str.split("#"); float res1 = new Float(results[0]); ``` but I'm not sure of the best way to get each float to 3 decimal places. I'd also prefer to do this in something neat like a for loop, but can't figure it out.