質問

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.

役に立ちましたか?

解決 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();

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top