android Database,textfields and saving form - android

How to store the double value into TextView and set the TextView with double result. I read all the articles but couldn't locate solution please Help.
I tried this one but it didn't worked
int x = qtyEditText.getInputType();
int y = rateEditText.getInputType();
double tot = x * y;
amountTextView = new TextView(this);
amountTextView.setText("" + tot);

Did you try:
amountTextView.setText(String.valueOf(tot)) ?
It works with other numeric types as well.

You can use Double.toString(tot);

Related

How to set LineSpacing programmatically?

i am new to Android Studio . I want to increase or decrease textView's line spacing on click. currently i am using this one and it works but i want when user click + than line spacing increased and decreased for - .
here is my code.
textView.setLineSpacing(0,1.1f);
i have tried this but not works
private int textSpace = (int) 1.0f;
private int diff = (int) 0.1f;
and than this
textSize = textSpace+diff;
textView.setLineSpacing(0,textSize);
same for minus, but its not working , please help
Here is a checked example. I hope everything is well described
// Here put ids of your views
TextView textView = findViewById(R.id.textView);
Button plusButton = findViewById(R.id.plus);
Button minusButton = findViewById(R.id.minus);
// The multiplier may be unchanged
final float multiplier = textView.getLineSpacingMultiplier();
// Set the appropriate offset
final float offset = 1f;
plusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() + offset, multiplier));
minusButton.setOnClickListener(view -> textView.setLineSpacing(textView.getLineSpacingExtra() - offset, multiplier));
I suppose you want to achieve something like this:
private int textSize = 20;
button.setOnClickListerner {
textSize += diff;
textView.setLineSpacing(0, textSize);
}

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 pass a double in .getText()

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

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