Question

I'm getting below error while converting String array to Long array.

java.lang.NumberFormatException: Invalid long: "5571.329849243164". How to round off this value like 5571.

Was it helpful?

Solution 2

//try this way, hope this will help you...

String value = "5571.329849243164";
Toast.makeText(MyActivity.this,""+Math.round(Double.parseDouble(value)),Toast.LENGTH_SHORT).show();

OTHER TIPS

Use this

String.format("%.2f", d)

round(Double value)`

see here

String string = "5571.329849243164";

    Double value1 = Double.parseDouble(string);

    tv.setText(Math.round(value1) + "/");

I understand you want to convert a String representing a floating point value to an Integer with rounding, not truncation. Perfectly reasonable.

Two choices.

  1. Convert the string to a Double first (Double.valueOf()), then convert to Integer (intValue()) using a rounding algorithm. Usually it's enough to just add 0.5 to the Double.

  2. Round the String using string operations, then convert to Integer. Split the string at the first ".", convert it to Integer (getInteger()). If the digit after the "." is in the range 5-9 then add one.

I'm sure you can write the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top