How can I round this result to 2 decimal places code example - android

How can i round off this particular code to show only 2 decimal places.
double sum = ((3.141592654/4*(tbmODInput)*(tbmODInput))-(3.141592654/4*(pipeODInput)*(pipeODInput)))*muckUpInput;
volume_per_meter_result.setText(String.valueOf(sum));

Kotlin
val value = 3.14159265358979323
// round to 2 decimal: 3.14
val formated = "Rounded is: %.2f".format(value)
Java
double value = 3.14159265358979323;
// round to 2 decimal: 3.14
String formated = String.format("Rounded is: %.2f", value);

For Java:
double value = 3.141592654;
String formated = String.format("Rounded is: %.2f", value);
For Kotlin:
val value = 3.141592654
val formattedText= "Rounded is: %.2f".format(value)

Related

String to Float Convert with decimal point Kotlin

I have String with decimal points of value. When I convert the String value to Float like this
val i : String = "123.70"
val j = i.toFloat()
Log.e("Tag", "-->" + j)
the result printed is
Tag-->123.7
But, what I want is
Tag-->123.70
Is there any possible way to get my desired result?
You can simply use a String that takes a decimal and formats it to a specific amount of decimal places.
Example:
fun main() {
// you have a String with a decimal value
val i : String = "123.70"
// then you parse/convert it to a Float
val j : Float = i.toFloat()
// and then, you output the value of the Float in a specific format
println("%.2f".format(j))
}
This prints
123.70
which should be working in the LogCat as well:
Log.e("Tag", "-->%.2f".format(j))
internally it doesn't affect it is the same but if you want to show it as 2 decimal places you can use String.format("%.2f", number).
val number = 10.7086134
val rounded = String.format("%.3f", number) // rounds to 3 decimal places

Kotlin float number with 2 decimals to string without precision loss

In Android-Kotlin I am getting float number from backend (for example num = 10000000.47)
When I try to String.format it and add that number in my balanceTextview it shows it with exponent (something like 1.0E10).
I want to show number normally without exponent and with 2 decimals. (Without presicion loss!)
Tried to use DecimalFormat("#.##") but it didn't help me. Maybe I'm doing something wrong?
num = 10000000.47f
val dec = DecimalFormat("#.##")
var result = dec.format(num)
my result is: 10000000
It losts my decimal places
The issue is your number type. According to the documentation:
For variables initialized with fractional numbers, the compiler infers the Double type. To explicitly specify the Float type for a value, add the suffix f or F. If such a value contains more than 6-7 decimal digits, it will be rounded.
With an example that shows how information may get lost:
val pi = 3.14 // Double
val e = 2.7182818284 // Double
val eFloat = 2.7182818284f // Float, actual value is 2.7182817
If the value is specified as Double instead of Float, i.e.
val num = 10000000.47
instead of
val num = 10000000.47f
then your approach works as expected, but could be shortened to:
"%.2f".format(num)
(note that the shorter version will also print "100" as "100.00" which is different from your approach but potentially still desired behaviour)
If you receive a Float from the backend then the information is already lost on your side. Otherwise you should be able to fix the issue by improved parsing.
The extension function format is only available in the JVM. In Kotlin/native, you can use this instead:
fun Float.toPrecision(precision: Int) =
this.toDouble().toPrecision(precision)
fun Double.toPrecision(precision: Int) =
if (precision < 1) {
"${this.roundToInt()}"
} else {
val p = 10.0.pow(precision)
val v = (abs(this) * p).roundToInt()
val i = floor(v / p)
var f = "${floor(v - (i * p)).toInt()}"
while (f.length < precision) f = "0$f"
val s = if (this < 0) "-" else ""
"$s${i.toInt()}.$f"
}

Kotlin: SumbyDouble returning additional decimals

I am summing double value from arraylist its giving additional decimals as 99999, how to fix this, please guide
ex
class ExDet{var expName:String ="",var expAmount:Double = 0.0}
val arrayList = ArrayList<ExDet>()
arrayList.add(ExDet("Abc 1",45.66))
arrayList.add(ExDet("DEF 1",10.0))
arrayList.add(ExDet("Lee 1",600.89))
arrayList.add(ExDet("Ifr 1",200.9))
var amt = arrayList.sumByDouble{ it.expAmount }
Expected Value of Amount is :
Amt = 857.45
But it returns
Amt = 857.4499999
Sample Code to Test
data class ExDet(var expName:String ="" ,var expAmount:Double=0.0)
fun main(args: Array<String>) {
val arrayList = ArrayList<ExDet>()
arrayList.add(ExDet("Abc 1",45.66))
arrayList.add(ExDet("DEF 1",10.0))
arrayList.add(ExDet("Lee 1",600.89))
arrayList.add(ExDet("Ifr 1",200.9))
var amt = arrayList.sumByDouble{ it.expAmount }
println("Amount is : $amt")
}
The issue you are confronted with is that floating point numbers are build on top of base 2, not base 10.
Think how you can easily represent a third as a fraction (1/3), but when you convert to decimal you get a repeating (recurring) number after the radix point (i.e. 0.33...). Some decimal numbers are recurring when represented in base-2, e.g. x.9. The computer has a finite number of bits, so the (base-2) number is truncated. All the truncation errors can add up.
You need to round to the required precision (e.g. round(x * 100) / 100).
If you are only interested in how it is displayed then you can use the format function with something like "%.2f".
String.format("%.2f", value)

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 How can I convert String to Double without losing precision? [duplicate]

This question already has answers here:
How can I convert String to Double without losing precision in Java?
(4 answers)
Closed 9 years ago.
i want to develop one calculation base application but getting one problem.
Tried as below
double add_num = 10.06
String data = edittext.getText().toString();
value assign to data is
// data = 1000.06
now i am converting string to double
double amount = Double.parseDouble(data);
// amount = 1000.0
double final_amount = amount + add_num;
// final_amount = 1010.0
getting final_amount is 1000.0 which is not correct because amount value is losing precision i want the correct answer which is 1000.06
please let me know correct way without using format() method
Well, first of all correct value is 1010.12, not 1000.06. And this code:
double add_num = 10.06;
String value = "1000.06";
double amount = Double.parseDouble(value);
double final_amount = amount + add_num;
System.out.println(final_amount);
prints 1010.1199999999999, which is correct.
If you just want to print the number with the desired precision, use one of:
// Prints with two decimal places: "1010.12"
System.out.format("%.2f", final_amount);
System.out.println(String.format("%.2f", final_amount));
// Example, set TextView text with two decimal places:
edittext.setText(String.format("%.2f", final_amount));
By the way, in your code you have:
String data = edittext.getText().toString();
double amount = Double.parseDouble(value); // value?
Shouldn't it be?
double amount = Double.parseDouble(data);
String s="1000.06";
double d=Double.parseDouble(s);
System.out.println(d);
The above code give output: 1000.06
You should use String.ValueOf(double d);
For Example:-
Double to String
String value=String.valueOf(1000.06);
OR
String value=String.valueOf(add_num);
String to Double
Double value=Double.valueOf("1000.06");
OR
Double value=Double.valueOf(add_num);

Categories

Resources