How to add two decimal numbers - android-studio

I have three edit texts in which if I put decimal numbers like 23.5 +23.5 and click calculate the app crashes and if I put 235000 +235000 it gives correct results what should be the correct codes for decimal number ,I am new to all this please forgive me for my mistakes`
#TargetApi(Build.VERSION_CODES.N)
public void onButtonClick(View v){
double tys=Integer.parseInt(Tys.getText().toString());
double lys=Integer.parseInt(Lys.getText().toString());
double tgt=Integer.parseInt(Tgt.getText().toString());
double sum=((int)tys-lys)/lys*100;
DecimalFormat precision = new DecimalFormat("0.0");
// dblVariable is a number variable and not a String in this case
GrowthResult.setText(precision.format(sum)+"%");
double sum2 =((int)tys/tgt)*100;
achievementResult.setText(precision.format(sum2)+"%");
double sum3 =((int)tys-lys);
JumpResult.setText(precision.format(sum3));
`

You can take data types like float type.then you will get correct answer
float a;
float b;
Edit text data parse in float type then you will get correct answer

Related

Groovy : String to float Conversion

Used code below to save value for float
domainInstance.standardScore = params["standardScore"] as float
In this case my input was given as 17.9 and in db2 database saving as 17.899999618530273 but I want to save as 17.9 itself, let me know how to do it
You can't set precision to a Float or Double in Java. You need to use BigDecimal.
domainInstance.standardScore = new BigDecimal(params["standardScore"]).setScale(1, BigDecimal.ROUND_HALF_UP);
The method BigDecimal.setScale(1, ...) limits decimal to one place only. The second parameter is the rounding strategy.
You need to use BigDecimal to do Conversion from String, then BigDecimal(value).floatValue() to get float, You can do this on more that one way, examples
1 - Using setScale in BigDecimal
def temp = new BigDecimal(params["standardScore"]).setScale(1, BigDecimal.ROUND_HALF_UP)
2- Using DecimalFormat
DecimalFormat df = new DecimalFormat("#.0");
def temp = new BigDecimal(df.format(params["standardScore"] ))
Then you need to get the float value
domainInstance.standardScore = temp.floatValue()

Retain 2 decimal points on float when converting to string delphi

I'm adding lines to a memo box from a sql query dataset. The data is being added using the memo1.lines.add function looping through the dataset
memo1.Clear;
frmMain.Query2.Open;
try
while not frmMain.Query2.Eof do
begin
memo1.Lines.Add(frmMain.Query2.FieldByName('mass').AsString);
frmMain.Query2.Next;
end;
finally
frmMain.Query2.Close;
end;
Everething from this point is working fine, the only problem is that because the data is being converted into string format is loses the trailing 0 .ie
Float value = 40.50 String Value = 40.5 .
Float value = 42.40 String Value = 42.4 .
Float value = 40.53 String Value = 40.53 .
Is there any way this could be rectified? FormatFloat doesn't want to format it because it's not a float anymore. StrtoInt doesn't work because of the 3 sting values in the memobox.
Instead of using AsString retrieve your values as Float values and the use FloatToStrF. E.g.
memo1.Lines.Add(FloatToStrF(frmMain.Query2.FieldByName('mass').AsFloat, ffNumber, 15, 2));
I like Format.
Format('%.2n', [frmMain.Query2.FieldByName('mass').AsFloat]);

Extracting integer and float numbers

I have the following string "10P_57.53%_568AA". I want to extract only numbers (integer and float numbers) without any other things. The output should be like this:
10 57.53 568
Since you've not provided any language, this would be general method:
Run a loop, get each character of string in a variable say X. Compare X as if(X>=0||X<=9||X=='.'){ Concat value of X to some string }

string variable returning as double

Create a method called parseEqn which will receive 1 String variable and return the double value of the expression passed to it.
parseEqn("123+23") → 146.0
parseEqn("3+5") → 8.0
parseEqn("3-5") → -2.0
so thats the question^^^^ and i think what i need to do is first use a string tokenizer to split the string up and then convert the tokens into doubles and from there add or subtract depending on the operator...but im not sure..
this is what i have so far
public double parseEqn(String str) {
StringTokenizer st = new StringTokenizer(str, "+-", true);
String first= st.nextToken();
String op= st.nextToken();
String second= st.nextToken();
double num1 = Double.parseDouble(first);
double num2 = Double.parseDouble(second);
if (op.equals("+")){
return num1+num2;
}
else (op.equals("-")){
return num1-num2;
}
i have no clue though....
Writing an expression parser is not a trivial task. The standard algorithm for parsing arbitrary infix expressions is the shunting-yard algorithm. The idea is to run through each token and build a Reverse Polish Notation (RPN) expression from the input. An RPN expression is essentially a stack-based list of operations that is very easy for a computer to work with (and easy to write code to evaluate).

Arduino issue: String to float adds two zeros instead of the correct integer

Code snippet:
Serial.println(sensorString); //so you can see the captured string
char carray[sensorString.length() + 1]; //determine size of the array
Serial.println(sizeof(carray));
sensorString.toCharArray(carray, sizeof(carray)); //put sensorString into an array
float sensorStringFloat = atoi(carray); //convert the array into an Integer
Serial.println(sensorStringFloat);
Serial.println(sensorStringFloat) prints out 5.00 instead of the correct float value of 5.33. Why is that and how do I fix this issue? I would eventually like to pass sensorStringFloat over to:
aJson.addNumberToObject(sensor, "ph", sensorStringFloat);
atoi converts a numeral in ASCII to an integer. The comment on that line also says it converts to an integer. So you got an integer result, 5. To convert to floating-point, consider using atof. (Note that “f” stands for floating-point, not “float”. atof returns a double.)
you should pass another parameter which defines the format, in this case it is the number of digits after the floating point.
Serial.println(sensorString,2);
String temp = String (_float, 0);
say float x;
convert to String using
String _temp = String(x, 0);
The second parameter 0... says i want no trailing zeros.
Caution: However this is only suitable for whole numbers.
This solution would not work for say... 1.24
You'll get just 1.

Resources