How to convert an integer value into floating point in java? - android

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);

Related

How can i show the value before the "." sign?

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 ","

Showing a percentage between 2 integers

I have a quiz game and i wanna show the percentage of level and maxlevel.
(levelvideo) is my first integer of current level
(QuestionLibraryVideo.mChoices.length) is my max level from length of table..
I try to show this = (levelvideo/QuestionLibraryVideo.mChoices.length)*100
but shows me only zero.
thsekato2.setText(Integer.toString((levelvideo/QuestionLibraryVideo.mChoices.length)*100));
IF you divide integer by integer, you will loose decimals after ,.
For example 3/2 is 1, beacuse it's 1,5, so integer have only 1.
Use float or double instead.
float f1 = levelvideo;
float f2 = QuestionLibraryVideo.mChoices.length;
zero.thsekato2.setText(Integer.toString((int)((f1/f2)*100)));
If you want to do this only with integer arithmetic, you can multiply the numerator by 100 before dividing. It doesn't have the precision offered by converting to a double, but then, it doesn't have the overhead of type casting either. This would look something like:
thsekato2.setText(Integer.toString((100*levelvideo)/QuestionLibraryVideo.mChoices.length));

Stored float value in string .. unable to convert it to integer

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")));

How to displaying Float/Double in meaningful form in Android

I am trying to make a BMI application. When I run the application the BMI values are displayed in numeral form that I don't understand. I have tried both Float and Double type but results are same.
For example:
Height (m): 2
Weight (Kg): 100
BMI is displayed as : 2.0E-4 instead of 25
The part of the code that effects this is:
String editText1= height_field.getText().toString();
String editText2= weight_field.getText().toString();
try { // Parse string to int
double height = Double.parseDouble(editText1);
double weight = Double.parseDouble(editText2);
double bmi_result = (weight/(height*height));
String bmi_text = Double.toString(bmi_result);
display.setText(bmi_text);
System.out.println("OnClick: computeButton is clicked");
}
catch(NumberFormatException nfe) {
alert.show(); // Show error alert
To answer your original question, you should be using java.text.DecimalFormat, something like:
DecimalFormat formatter = new DecimalFormat("##.##");
display.setText(formatter.format(bmi_result));
Will force the result to be in the format of two digits followed by two decimal points, the table in the link above shows how to generate that.
However, since 2.0E-4 is 0.0002, I think Jon Skeet's comment is correct: You're doing your math operation wrong, since the value you're printing is a very small fraction of 25 :)
I'd recommend using Log.v() to print out your math operation before you actually do it, so you can see what the values of weight and height actually are, I highly doubt they're correctly set at what you described in the question.

how adjust double value to approximate value after

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)

Categories

Resources