how adjust double value to approximate value after - android

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)

Related

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

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

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

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.

java Double.valueOf() returns number with one decimal place

I can't seem to get my head around this. If have tried the following approaches:
DecimalFormat df = new DecimalFormat("00000.00");
DecimalFormat df = new DecimalFormat("######.##");
But the following line always generates and IllegalArgumentException.
double price = Double.valueOf(df.format(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString()));
// Sample input passed is value of 200 or some other whole number.
item.setPrice(price);
It doesn't make sense as I only copied the obvious solutions in this forum. Most of you got the format() to work.
Originally, I didn't have these lines of code. I just call my setPrice() method after getting the item price. This works. However, Double.valueOf() has a nasty habit of using only one decimal position.
e.g. passed 200. I get 200.0 inside my item object. I figured by using DecimalFormat I could've prevented this but it appears this caused me MORE headaches instead.
When you say you pass 200 and you get 200.0, you mean you get it in a double value? If so, that doesn't matter - it's a number and 200 = 200.0 for double values.
format(...) turns a double value to a String value. You have it the other way round. That's why you get the Exception.
If the price variable is actually a double you should do
double price = Double.valueOf(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString())
But I think you want that the price is a String, then you should convert the text from the EditText to a double and that double back to a String with something like new DecimalFormat("0.00")

Android display float using settext method

iam allmoust done with my app but iam stuck on this thing , i have two int's
called "positive" and "negative" and when i procces source below it shows 0.0
total = positive + negative;
float rate = positive/total;
rate*=100;
TextView analitycs = (TextView)v.findViewById(R.id.app_scores_analitycs);
analitycs.setText(String.valueOf(rate));
What Victor said is true.
Also you might want to use something different than String.valueOf(rate) to set the text of your text view, because this method can give you an ugly representation of the number.
You should probably use String.format("%.2f", rate) ad tweak that to your needs.
Are positive and total floats/doubles?
If not, then an int/int will give you an int.
The solution would be to cast either positive or negative as a float.
try the following:
float rate = ((float)(positive))/total;

Categories

Resources