I have a string value which ids getting from JSON. Value is like 10.95. But i want to convert it to Integer. How can I convert?
Not possible to convert directly to Integer.Use Math.round() before typecasting using (int) should round the float to the nearest whole number.
int value =(int)(Math.round(Float.valueOf("10.95")));
Related
I'm creating a currency application but some of values are like "194.23564" or "1187.7594" so i want to show the user before the "." sign values. How can i make this with Kotlin ?
There is no need for data type conversion before the extraction of the integer part.
You can use substringBefore():
val number = "194.23564"
val intPart = number.substringBefore(".")
If you want the result as an integer number you can use now toIntOrNull(), instead of toInt(), so to avoid an exception in case the initial string has no integer part (like ".015"):
val intPart = number.substringBefore(".").toIntOrNull()
Other than suggested, I would not convert to Float. This is susceptible to rounding errors and may not return the value before the decimal point.
Example:
val num = "0.99999999"
println(num.toFloat().toInt()) // gives 1
Instead, split the string at the decimal point:
val num = "0.99999999"
val split = num.split('.')
println(split[0]) // gives 0
A nice side effect of this implementation is that it even works for integral numbers without a decimal point. If you need the result as an Int, simply call split[0].toInt().
There is no need to use float in this case. If you want to get the value before ".", you need to use int instead of float. When you use float you will get value in points, but when you use int, you will get the value before ","
I cannot restrict the TextView to show only 3 decimals when I'm using a variable from another class. Here's the problem:
I have this TextView
public static TextView pt_resultado;
However when I tried to use it to show the number inputted from another screen while converting from Double restricting the decimals:
pt_resultado.setText(String.format("%.3f",(Double.toString(ActivityPopulacao.pt_resultado2))));
I get the following error:
java.util.IllegalFormatConversionException: %f can't format java.lang.String arguments
Is there a way to show only three decimals, put without changing the conversion?
You are trying inserting string as float.
You should do:
pt_resultado.setText(String.format("%.3f",Double.valueOf(ActivityPopulacao.pt_resultado2.getText().toString())))
You need to:
Retrieve String from TextView.
Convert String to Double/Float.
Format Double/Float value as String at 3 decimal places.
I assumed that ActivityPopulacao.pt_resultado2 is also TextView!
To help you also understand why this is happening ill break it flow a bit.
double recievedNum = ActivityPopulacao.pt_resultado2;
String convertedNumAsString = String.format ("%.3f", recievedNum);
pt_resultado.setText(convertedNumAsString);
Can you see the mistake? Your using Double.toString(number) where its expecting a number. String.format takes in the double does formatting and converts to string for you.
Defining TextView as a static view will cause memory leaks. Going forward, you can pass value from one activity to another using Intent - I presume you're using activity. Also, getText().toString() has already converted the input value to string. So you don't need that Double.toString() any more. Copy below code and paste it in the code that is responsible for starting the new activity.
Intent in = new Intent(ActivityPopulacao.this, NewActivity.class);
in.putExtra("key", pt_resultado2.getText().toString());
startActivity(in);
To receive the value, get the intent and request for the string in your NewActivity onCreate() method.
String newDouble = getIntent().getStringExtra("key");
pt_resultado.setText(newString);
Note that NewActivity in the above snippet refers to the activity that needs the value.
Why do you convert to string and try to format it like it is a Double?
The format() method expects a number to format.
Since ActivityPopulacao.pt_resultado2 is Double you should format the double value:
pt_resultado.setText(String.format("%.3f", ActivityPopulacao.pt_resultado2));
changeweek = (Map<String,ArrayList<Integer>>)dataSnapshot.child("week").getValue();
ArrayList<Integer> test = changeweek.get("Monday");
Log.d("changeweek",changeweek.toString());
int j = test.get(2);
I get an error in the last line which is the following:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at com.example.fake9.tendee.ScheduleActivity$1$1.onDataChange(ScheduleActivity.java:107)
I don't know how this happens since I am storing Arraylist of integers into the hashmap. The following is a picture of the database.
The Firebase SDK internally stores all integer-like number values as Long values, whether or not you want. This helps defend against possibly very large numbers as values.
Your cast to a Map with values of type Integer is overriding that, then causing problems at runtime when the types don't match. You can correct this by simply changing your value type from Integer to Long.
Rather than directly converting long to int, convert long to string using String.valueOf() then we can easily convert string value to int using Integer.parseInt()
So you can go with this,
**
int j = Integer.parseInt(String.valueOf(test.get(2)));
**
I am getting an integer value in my android application.I want to convert it into floating point number which is in this format
"0.xyF"
.I tried lot of methods.I know its simple but i am confused.Please help.
I am passing a value from one activity to another using putExtra.So in the second activity i have to convert it to float for setting the value as verticalMargin for my dialog window.I used this line for getting the value in second activity.
int data = getIntent().getIntExtra("value", 7);
This is used for setting the vertical margin.
wlp.verticalMargin = "the converted floating point number";
If i is the integer value, then try:
float f=i;
while(f>=1.0f)
f/=10.0f;
You question still isn't clear. If you're asking how to convert an integer value that represents a percentage from 0 to 100 into a floating point value, then it would be fpVal = intVal / 100.0;
If you just want a simple conversion of an integer into a floating point number with the same exact value (e.g. 7 --> 7.0), then you can just cast it: fpVal = (float) intVal;
In your first activity store your integer value in string like this
String margin = "0."+int_value;
then pass this string to second activity.
In the second activity get that string from extra and convert it to float.
float float_value = Float.parseFloat(margin);
i am getting double values,i need to reduce to approximative value after . for example
i have 123.678,i need to format this as 124.
if i have 123.212 i need to format this as 123
how can i done this,can any one please help me.
Thank u in advance.
Sounds like you want Math.round() for floats or Math.round() for doubles
public static int round(float a)
public static long round(double a)
Returns the closest int/long to the
argument.
The result is rounded to an
integer by adding 1/2, taking the
floor of the result, and casting the
result to type int/long.
In other words,
the result is equal to the value of
the expression: (int)Math.floor(a + 0.5f) or (long)Math.floor(a + 0.5d)