Floating Point with 3 Digits after Point - android

Here is my button on click method. here a input 3 floating number. after calculation i want to show my result with 3 digit after point.It is shown in 3 text field. how can i do . ?
public void cal(View V){
float dRa,dRb,dRc,z21,z12,z11,z22;
dRa = Float.parseFloat(ra.getText().toString());
dRb = Float.parseFloat(rb.getText().toString());
dRc = Float.parseFloat(rc.getText().toString());
//Result=dRa+dRb+dRc;
z21 = ((dRa*dRb)/(dRa+dRb+dRc));
z11=(dRa*(dRb+dRc)/(dRa+dRb+dRc));
z22=(dRb*(dRa+dRc)/(dRa+dRb+dRc));
Z21.setText("Z21 & Z12 are "+z21);
Z11.setText("Z11' is "+z11);
Z22.setText("Z22' is "+z22);
}

Just did that today with 2 fraction digits. here's how I did it (though I used a double and not a float)
NumberFormat format = NumberFormat.getNumberInstance();
format.setMinimumFractionDigits(0);
format.setMaximumFractionDigits(3);
Z21.setText(format.format(Z21));
Z11.setText(format.format(Z11));
Z22.setText(format.format(Z22));

Related

Square Root formula in Calculator

Ok I am having an issue trying to put a square root equation in a calculator. I am trying to figure out the expression builder. I know the expression builder takes the math operations of add, subtract, multiply, divide, equals and the parenthesis. What I am doing is trying to build the square root section. I have a simple Percent code to help with the square root.
In the Square root vs the Percent you see I am using binding. So here is the code for both.
On the square root is it possible to use the expression builder? I know there is no absolute formula for square root except for a number that is multipliable with itself like the number 4.
Sqrt(4) = 2
binding.btnSqrt.setOnClickListener {
var square = (tv_equation.text.toString().toDouble() / 2)
binding.tvResult.text = square.toString()
}
So in the event you a non square equation
sqrt(23) = 4.79
How would I simulate that as one function within the button. Can I use expression or would I need to use Kotlin.math
So between the two I divide by 100 on the percent. It works great.
binding.btnPercent.setOnClickListener {
var percentage = (tv_equation.text.toString().toDouble() / 100)
binding.tvResult.text = percentage.toString()
}
All my other buttons work fine and so I am just working on the square root before I can release this to google play.
You would need to use some form of a square root function.
AS you mentioned in your question Kotlin Sqrt Function is a very suitable choice
binding.btnSqrt.setOnClickListener {
if(!tv_equation.text.isNullOrEmpty){
var number = tv_equation.text.toString().toDouble()
binding.tvResult.text = sqrt(number).toString()
}
You can create a sqrt function using the Quake's first inverse square root.
Quake's first inverse square root.
Pseudo Code:
float InvSqrt(float x){
float xhalf = 0.5f * x;
int i = *(int*)&x; // store floating-point bits in integer
i = 0x5f3759df - (i >> 1); // initial guess for Newton's method
x = *(float*)&i; // convert new bits into float
x = x*(1.5f - xhalf*x*x); // One round of Newton's method
return x;
}
So the answer i want to add is a little more suitable with the previous answer. Maybe this will help some people in the future. This solution will also limit the decimal space to 4 decimals.
binding.btnSqrt.setOnClickListener{
val df = DecimalFormat("#.####")
if(!tv_equation.text.isNullOrEmpty())
{
val number = tv_equation.text.toString().toDouble()
binding.tvResult.text = df.format(sqrt(number))
}
}
you can adjust the val df = DecimalFormat("#.####") where the # to as many decimals as you would want.

getting result of decimal number

I am trying to divide two integer and get a decimal number
I am keep getting 0 result when I divide 10/29
I would like to get 0.34
my code is :
private int totalCount_Games;
private int totalCount_Hints_Statistics;
double avgHints;
avgHints=totalCount_Hints_Statistics/totalCount_Games;
In Java, when you divide two integers, the result is another integer.
In your case, 10/29 will result in 0 as you mentioned. If you want to get the results of these in floating digits, then change the above two integers to float or double.
In that case, the result for the above calculation will be 0.34.
PS: This is really basic. You should do more research in the official java site for documentation on datatypes.
The result of a int division is another int, rounded.
Casting int to double (or float) in the expression part will make the division occurs on doubles instead of int, note that this is different from casting the result from the int division to double.
int i = 5, b = 10;
double result = ((double)i)/((double)b);
result is 0.5
The above code will result in 0.0 as int/int will always be a int which is further type casted into double thus output is 0.0
Use the below code, Using big decimal for Rounding
double totalCount_Games_double = totalCount_Games;
double totalCount_Hints_Statistics_double = totalCount_Hints_Statistics;
double value = totalCount_Hints_Statistics/totalCount_Games_double;
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, RoundingMode.HALF_UP);

Show expanded float value in Android

I have a function that simply converts units, and my goal is for the output number to be displayed in expanded form, as in showing the full number, not "3.0E7". The code is as follows:
public void convertUnits(View view)
{
EditText fromNumberEditText = (EditText)view.findViewById(R.id.fromField);
TextView toNumberEditText = (TextView)view.findViewById(R.id.toField);
String fromUnitString = selectedList[selectedLeftItem];
String toUnitString = selectedList[selectedRightItem];
float fromUnit = Float.parseFloat(fromUnitString.split(",")[2]);
float toUnit = Float.parseFloat(toUnitString.split(",")[2]);
float fromNumber = Float.parseFloat(fromNumberEditText.getText().toString());
float toNumber = (fromNumber * fromUnit) / toUnit;
toNumberEditText.setText(Float.toString(toNumber));
}
The toNumberEditText has marquee, and because of that I would like for it to show the expanded number, i.e. 30000000 not 3.0E7. Whenever the toNumber float is anything more than 7 digits it assumes the "floating e" form. Is there a way to either change the amount of digits before it does this, or remove it altogether?
Use String.format():
toNumberEditText.setText(String.format("%f", toNumber));

How to get the position of selected character or String in EditText

I want to know how can I get the x,y position of the selected character or String in
EditText. Is that possible?
Use this code to get the first index of a certain character:
String s = editText.getText().toString();
int position = s.indexOf("C"); // where C is your character to be searched
Here is how to get the x and y coordinates of a specific character in a TextView, should work for EditText as well. offset is the index of the desired character in the view's text.
Layout layout = editView.getLayout();
if (layout == null) { // Layout may be null right after change to the view
// Do nothing
}
int lineOfText = layout.getLineForOffset(offset);
int xCoordinate = (int) layout.getPrimaryHorizontal(offset);
int yCoordinate = layout.getLineTop(lineOfText);

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