I already searched online and all that I found is using DecimalFormat and I tried it, but when I code on Android Studio, appears a message saying that is necessary to use the API 24 to this kind of command. The API 24 has erros and all the sites I looked, advised to use the API 23.
So, I need a way to show double numbers only with two decimal digits on my AlertDialog.
referring to this SO post, you can use this code:
String formattedValue = String.format("%.2f", myDouble);
Let me know if it is what you were looking for
/** Round on two digits after "." */
public static float round(float f){
return Math.round(f * 100) / 100.0F;
}
P.S. Use float for short float numbers. Thats what its made for.
Edit:
"Ok, with 3 decimal digits its the better too,right? How can I limit those digits?"
Take a quick look how it works for two decimal places. Then its really simple to expand for three.
Related
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.
We have problem in our Android application with rounding numbers. In calculation made in Java we use BigDecimal and this helps, but we have problem with SQLite, for ex.
SELECT ROUND(150.075 * 100) / 100 (= 150.07, not 150.08!)
What you think about this (not elegant) solution:
SELECT ROUND(150.075 + 0.00000000000009, 2) (= 150.08)
We checked it on more then 5 000 incorrect values and it's works good.
That's because due to floating point inaccuracies, 150.075 * 100 is not the same as 15007.5. The decimal representation for 150.075 in double-precision floating point is 150.07499999999999999722444243843711.
To get the behavior you want, use the two-argument version of ROUND() which lets you specify the number of decimal digits:
sqlite> SELECT ROUND(150.075, 2);
150.08
I have a small application that converts temperature from Celsius to Fahrenheit and vice-versa. I have two RadioButtons to select the temperature i.e. Celsius or Fahrenheit and one Button and one EditText . I created a Junit to test the application.
Here is my test code:
float temparature=102;
Float expected=(float) 0;
solo.enterText(0,String.valueOf(temparature));
solo.clickOnButton("calculate");
if(solo.isRadioButtonChecked(0)){
expected=((temparature-32)*5/9);
}
else if(solo.isRadioButtonChecked(1)){
expected=((temparature*9)/5)+32;
}
String temp=expected+"";
assertEquals(true,solo.searchEditText(temp));
}
When I run the above test, test run was successful but failed saying: expected<true>but was <false>. I think there is some problem with value rounding. Please let me know what exactly is the problem.
You have String temp=expected+""; but expected is object(Float) type - Float expected.
So try expected.toString() or change Float expected to float expected.
And try to debug.
The value of expected is probably something like 102.0000001, which is not going to match the text in solo, which should be 102 (if I understand the code correctly).
You should follow the standard float comparison techniques instead of comparing Strings.
http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals%28java.lang.String,%20double,%20double,%20double%29
So you'd use something like:
assertEquals("The converted temperature didn't match.", temparature, expected, 0.001);
Overall, it isn't clear what your test case is trying to validate exactly. And the title of your post doesn't indicate anything about the question.
in android eclipse sometimes a calculation result for both double and float when displayed as a string uses a decimal point (desired) but sometimes using an exponent (bad - confusing to user). anyway to avoid the exponent?
See String.format documentation.
Just set the desired format for your numbers. You probably want String.format("%f",number).
I am trying to make a program that takes some user input, runs a few calculations and outputs the answer. My problem is that this answer is sometimes many decimal places long which is causing some aesthetic and layout problems. I only need to display 4 decimal places worth of data. Is there anyway to limit the precision of these numbers at output time? (The Numbers are stored in floats and I'm programming for Android.)
You can format a float to 4 decimal places using String.format.
Example:
String result = String.format("%.4f", theNumber);
See also:
How to nicely format floating numbers to String without unnecessary decimal 0?
String.format(format, args)
Format strings in Java