How can I round numbers (2 decimal) parseXml get(field) in Android - android

TextView.setText(XMLdata.get("XMLField") + " mm");
input values that are expressed in an abbreviated way:
10,5858555966668 (with comma)
Which I would like to convert into:
10,58

if your XMLdata.get("XMLField") value is float u can do the following code
String finalValue = String.format(
"%.2f", XMLdata.get("XMLField"));
TextView.setText(finalValue + " mm");
if your XMLdata.get("XMLField") is String convert it into float do the above method.

TextView.setText((Math.floor(MathXMLdata.get("XMLField") * 100.0) / 100.0).toString() + " mm");
This converts 10,5858555966668 to 1058,58555966668, rounds it to 1058 and converts it back to 10,58.
Note: Math.floor always rounds down (cuts off all missing fractional numbers). If you intend to round to the closest value, use Math.round.

Thank you, thank you very much rmkrishna and Faro ((= I do it with
TextView.setText(String.format("%.2f",Double.parseDouble(XMLData.get("Son7GunlukSagis").replace(",", ".")))+ " mm");

Related

Sum is giving a negative number in Kotlin

So here is my Kotlin Code:
val intBoth = 1_391_000_000 + 1_432_342_859
println("China plus India: " + intBoth)
And the result is negative:
China plus India: -1471624437
What went wrong here?
If you you don't select a your variable type (like Long,Float,Bytes,Shorts), the kotlin asumes it is an Integer.
The variable types have boundary conditions for representing number values.
The boundary of Integers are between -2,147,483,648 and 2,147,483,647 (It's about byte of size). The variables of your calculation of summiation are Integer so the complier expect the total value is Integer, but the result is out of the range of Integer variable type. Yo can overcome the problem by convert to Long your types of elements. Write .toLong end of the variables for converting
fun main() {
val intBoth = 1391000000.toLong() + 1432342859.toLong()
println("China plus India: " + intBoth)
}
Also you can use L instead of .toLong().
val intBoth = 1391000000L + 1432342859L

Decimal point issue in displaying digits

I have to display digit in two decimal points.
Let say value 0 should display as 0.00 and value 2.3 shoule display as 2.30.
To achive this I have done something like below :
Log.e(">>> percent ", ">> " + data.percent)
percent = String.format("%.2f", percent).toDouble()
Log.e(">>> percent ", ">> " + percent)
But the result you will notice is as below :
2021-04-15 11:55:54.580 10061-15047/com.dev E/>>> percent: >> 0
2021-04-15 11:55:54.580 10061-15047/com.dev E/>>> percent: >> 0.0
It should be 0.00 instead of 0.0
What might be the issue?
Please guide. Thanks.
Double type does not have information about how to display.
So, do not use toDouble() method to the string.
Also, add 0 to the format string to denote the zero padding.
Log.e(">>> percent ", ">> " + data.percent)
val percentStr: String = String.format("%.02f", data.percent)
Log.e(">>> percent ", ">> " + percentStr)
You can use DecimalFormat https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
val df = DecimalFormat("0.00")
df.format(percent)
just change
String.format("%.2f", percent).toDouble()
to
String.format("%.02f", data.percent)

How to remove digits after decimal in a double?

my double = 42.12323532 I want it to show just "42". How do I do that? This is what I have tried-
TV_BMR.setText("BMR: " + String.format("%.2f", BMR) + " cal");
but this only rounds it to 2 digits. I want everything removed after the decimal. Including the decimal.
String str = “BMR: “ + (int)BMR;
String str = “BMR: “ + (long)BMR;
Then we can, In the high double type range numbers everything removed after the decimal.

How to show numbers in these formats Android

Example :
value = 0.0
I want to covert it to
Ans :value = 00.0
example
value = 12.0
Ans: 12.0
example
value = 1.0
Ans: 01.0
Right now for float values i am using String.format( " %.2f" ,variable_value)
is there any way like this to convert values
Thanks
you can use string.format method like below:
String value= "_%02d" + "_%s";
it converts numbers like :
String.format("%02d", 1); // => "01"
you can see forrmatter doc for other modifiers

android mathematical calculations in eclipse java

Can anybody tell me why the result of the following expression is -4.91679930495
9.81*Math.cos(phi)/Math.sin(phi)
if phi = 90°?
In particular, I want to display the value of acceleration.
tvText2.setText("acceleration: " + String.valueOf(9.81*Math.cos(phi)/Math.sin(phi)) + " m/s²");
Acording to docs http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#cos(double):
The paremeter of those methods is in radians.
you will need to call Math.toRadians that receives an angle in degrees. http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#toRadians(double)
Convert phi to Radian before calculating acceleration:
double phiInRad = Math.toRadians(phi);
tvText2.setText("acceleration: " + String.valueOf(9.81*Math.cos(phiInRad)/Math.sin(phiInRad)) + " m/s²");
or in one line
tvText2.setText("acceleration: " + String.valueOf(9.81*Math.cos(Math.toRadians(phi))/Math.sin(Math.toRadians(phi))) + " m/s²");

Categories

Resources