How to pass a double in .getText() - android

I'm trying to create an app that converts celsius to Farhenheit. I want to pass float in .getText() but I'm unable to do so, It is giving me error which say cannot invoke .getText() on the primitive time float. Like I want to take the user input as double and then do the maths calculation. Can you please suggest me the approach to achieve this.
changer = Float.parseFloat(textIn.getText().toString());
textOut.setText(changer);

First you want edittext.gettext().tostring. And String Convert to double.

txtProt = (EditText) findViewById(R.id.Protein);
p = txtProt.getText().toString();
protein = Double.parseDouble(p);

float celsius = Float.parseFloat(textIn.getText().toString());
float farhenheit = (celsius * 1.8f) + 32.0f;
textOut.setText(""+farhenheit);
in the case of converting from farhenheit to celsius:
case(R.id.radioButton2):
float farhenheit = Float.parseFloat(textIn.getText().toString());
float celsius = (( farhenheit - 32.0f ) x 5.0f ) / 9.0f;
textOut.setText(""+celsius);
break;
NB : you should add break; at the end of each case in the switch bloc

i am currently not able to test this but i think this is the answer u need
String text = edittext.gettext.toString()
double f = double.parseDouble(text);
double f2 = do the math to calculate farhenheit
textview.settext(f2.toString())

Related

Rounding to two decimal places in Android

Should be simple I know but I cant find an answer anywhere. I'm trying to round up to two decimal places, so if my answer is 164.9835 I'd like the answer to be displayed as 164.99. But what I have so far is rounding it to 164.98 for some reason.
Any help much appreciated.
double number1 = Double.parseDouble(num1.getText().toString());
double number2 = Double.parseDouble(num2.getText().toString());
double number3 = Double.parseDouble(num3.getText().toString());
double number4 = Double.parseDouble(num4.getText().toString());
double sum = (((number1 * number2)/1000)*0.5)*(number3 - number4);
total.setText (String.format("£%s", new java.text.DecimalFormat("##.##").format(sum)));
If you want to round up you can use this method
cantDecimal = 2;
number = 164.9835
public static double aroundUp(double number, int canDecimal) {
int cifras = (int) Math.pow(10, canDecimal);
return Math.ceil(number * cifras) / cifras;
}
return = 164.99
Extra: Ceil Method in Math.
The method ceil gives the smallest integer that is greater than or equal to the argument.

Why I can not see the double value in a textView

Why I can not see the double value in a textView?
textView = (TextView) findViewById(R.id.textView);
double x = 5/2;
textView.setText(String.valueOf(x)); // I see this result as 2.0 and not as the 2.5
In Android TextView, you can use in that way,
Use Double.toString:
result = number1/number2
String stringdouble= Double.toString(result);
textview1.setText(stringdouble));
or you can use the NumberFormat:
Double result = number1/number2;
NumberFormat nm = NumberFormat.getNumberInstance();
textview1.setText(nm.format(result));
To force for 3 units precision:
private static DecimalFormat REAL_FORMATTER = new DecimalFormat("0.###");
textview1.setText(REAL_FORMATTER.format(result));
You are seeing 2.0 because 5/2 is integer division. Change the line to double x = 5.0/2; or similar.
See this question for more.
This should work double x = ((double) 5) /2.

how to use a double value in power function

i am trying to find the power of a value.But the problem is my exponent is a fractional value.power function does not suppporting any datatype other than int.
BigDecimal fd_returns_at_time_of_replace=(BigDecimal.valueOf(capitalDiff).multiply((BigDecimal.valueOf((long)constant1+.09)).pow(temp)));
here temp is a fractional value.given below is the eror message i am getting.
The method pow(int) in the type BigDecimal is not applicable for the arguments (double)
please anybody help me to do this.
BigDecimal.pow() only takes an int. To see a cool example of writing BigDecimal.pow() that accepts a double, see this question How to do a fractional power on BigDecimal in Java?
Common Sense
Consider you want to raise the number x to the power y
If both are integers:
for(int i=0 ; i<y ; i++)
answer = answer * x;
Problems are only when y is a decimal!
So we first change y to the form of y = n + 1/d
How to do that:
n = floor of y
d = 1 / (y - n) << integer
Now x^y = x^n * x^1/d
x to the power n is simple using the usual method
x to the power 1/d is simply the d th root of x
Note: You can increase the precision of your function by reducing the error factor induced by makind d an integer. How! 1/d can be multiplied by powers of 10.

how to convert EditText control's value in to float value in android

im receiving some value from EditText control. and i want that value for some calculation so i need to convert it to float value how can i do that?
i tried below But getting null pointer exception:
float ip1=Integer.getInteger(t1.getText().toString());
Any Soln?
Try this:
String str = t1.getText().toString();
Float ip1 = new Float(str);
System.out.println("Float value = "+ip1);
float ip1=new Float(t1.getText().toString());

Floating Point with 2 Digits after Point

I will Format a Floating Point Number in android with 2 Digits after the point.
I search for a Example but i din´t find one.
Can some one help me.
float f = 2.3455f;
String test = String.format("%.02f", f);
You can also use DecimalFormat
float f = 102.236569f;
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(f)); // output is 102.24
//double twoDigitsF = Double.valueOf(decimalFormat.format(f)); also format double value
In answer above it always will return 2 deciaml
examples with String.format("%.02f", f)
2.5555 -> 2.55
2.5 -> 2.50
but do you need to get 2.50 for 2.5?
for avoid to get excessive 0, you can use this solution
val format = DecimalFormat("0.##")
return format.format(floatNum)

Categories

Resources