Displaying value of a double variable without Scientific Notation - android

I have two variables with datatype double, when i multiply those variables they return result in scientific form but my requirement is to display result in normal human readable form.
For Example:
when i multiply 9854795 and 8.9 it returns result as 8.77076755E7 instead of 87707675.5
here is my function for calculating values:
private void calculatingTotalPaymentAmount() {
if (editTextBookingQuantity.getText().toString().equals(""))
{
editTextBookingQuantity.setError("Enter Booking Quantity to calculate Total Payment Amount");
return;
} else if (editTextRatePerBrick.getText().toString().equals(""))
{
editTextRatePerBrick.setError("Enter Rate Per Brick to calculate Total Payment Amount");
return;
} else {
//MathContext mc = new MathContext(4); // 4 precision
final double catchBookingQtyDoubleForm = Double.parseDouble(editTextBookingQuantity.getText().toString());
final double catchRatePerBrickDoubleForm = Double.parseDouble(editTextRatePerBrick.getText().toString().trim());
final double totalPaymentAmount = (catchBookingQtyDoubleForm * catchRatePerBrickDoubleForm);
//bg3 = bg1.multiply(bg2, mc);
//BigDecimal newValue = totalPaymentAmount.setScale(0, RoundingMode.DOWN);
editTextTotalPaymentAmount.setText(""+totalPaymentAmount);
}
}

Log.d("tag", "" + new BigDecimal(catchBookingQtyDoubleForm * catchRatePerBrickDoubleForm));

Related

What's the easiest way to convert a String such as "5:4" to a numerical value

I a working with Strings with the format I've mentioned such as "5:4" and I need to get their value (for example "5:4" equals 1.25).
A simple .toDouble(), what would be the best approach
You could Iterable.reduce() :
println(
a.split(":").map { it.toDouble() }.reduce { a, b -> a / b }
) // 1.25
A method with plain Kotlin using the String.split() method could look like this:
// INPUT
val ratio = "5:4" // can be "5/4" as well
// splits given string into a list and then cast it's members into Double
val items : List<Double> = (ratio.split(":", "/")).map { it.toDouble() } // [5, 4]
var result = 0.0
// calculate the ratio
if (items.size == 2 && items[1] != 0.0)
result = items[0] / items[1]
println(result) // 1.25
String a="5:4";
a= a.replaceAll(":", "/");
float result = new Expression(a).eval();

calculate percentage and display in a texview android studio

I am developing an application where I pass 1 variables int through a string of an activity to next activity, in the next activity I take that string and return it again and an int, then I calculate a percentage and display and a textview, the past variable and approx1 so I check if it is not empty, then I calculate the percentage ex: ((3/45) * 100) and display in a text view, reviewing again for string ... but in any way I make a mistake, what can be ?
Bundle bundle = getIntent (). GetExtras ();
String aprox1 = bundle.getString ("aprox1");
if (aprox1! = null)
try {
num3 = Integer.parseInt (aprox1);
 result = Math.round ((num3 / 45) * 100);
 TextView counter2 = (TextView) findViewById(R.id.textView16);
String abcString2 = Integer.toString (result);
counter2.setText (abcString2);
}
 catch (NumberFormatException e) {
 }
You need to have {} after the if statement like so:
if (aprox1! = null) {
try {
num3 = Integer.parseInt (aprox1);
result = Math.round ((num3 / 45) * 100);
TextView counter2 = (TextView) findViewById(R.id.textView16);
String abcString2 = Integer.toString (result);
counter2.setText (abcString2);
}
catch (NumberFormatException e) {
}
}
Its also worth noting that because num3 is an integer when you divide it by 45 you will get an Integer not a percentage.
To fix this issue, make num3 a double or cast either num3 or 45 to a double before computing the division.
For example, a simple fix would include changing line 4 to the following:
result = Math.round ((num3 / 45.0) * 100);

Remove digits which are in the decimal place

I get a double value (eg. -3.1234) which contains a value in decimals (0.1234 in this case). I want to remove the value in decimal value (in this case I wanted the value -3). How do I do it? I know how to remove the decimal if the decimal only has 0, but in this case I don't have the decimal value as 0.
Anyways here is the code for that:
DecimalFormat format = new DecimalFormat();
format.setDecimalSeparatorAlwaysShown(false);
Double asdf = 2.0;
Double asdf2 = 2.11;
Double asdf3 = 2000.11;
System.out.println( format.format(asdf) );
System.out.println( format.format(asdf2) );
System.out.println( format.format(asdf3) );
/*
prints-:
2
2.11
2000.11
*/
If you just want to print it :
String val = String.format("%.0f", 2.11)
// val == "2"
If you want to keep the double type but without decimals :
double val = Math.floor(2.11);
// val == 2.000
If you don't care about type you can cast the double into int :
int val = (int) 2.11;
// val == 2
//double as object
int val = myDouble.integerValue();
You can simply use the intValue() method of Double to get the integer part.
You can cast it to int
int i=(int)2.12
System.out.println(i); // Prints 2
Round up decimal value & convert it into integer;
Integer intValue = Integer.valueOf((int) Math.round(doubleValue)));
http://www.studytonight.com/java/type-casting-in-java check out type conversion in java
double asdf2 = 2.11;
int i = (int)asdf2;
System.out.println(asdf2);
System.out.println(i);
Output:
2.11`
2

How to add Comma between numbers

I have this code for my calculator:
public void onClick(View v) {
try {
double price = Double.parseDouble(InputPrice.getText()
.toString());
double percent = Double.parseDouble(InputPercent.getText()
.toString());
double priceValue = price * percent / 100.0f;
double percentValue = price - priceValue;
PriceToGet.setText(String.valueOf(priceValue));
PriceToPay.setText(String.valueOf(percentValue));
PriceToGet.setText(String.format("%.02f", priceValue));
PriceToPay.setText(String.format("%.02f", percentValue));
The Input and the Output are coming without commas like this:
Input: 333333333
Output: 134555.44
Output: 17475.66
This was just an example for Output and Input.
How do I like the user see them is:
Input: 333,333,333
Output: 134,555.44
Output: 17,475.66
Thanks
Update:
I added decimal in my onclick code:
DecimalFormat formatter = new DecimalFormat("#,###,###");
I used this code but its closing the App after I press the button:
String PriceToGet = formatter.format(String.format("%.02f", priceValue));
And when I am using this method:
String PriceToGet = formatter.format("%.02f", priceValue);
Its force me to change it to:
String PriceToGet = formatter.format(priceValue);
What to do?
You need to use DecimalFormat
You will find the complete answer here
This is how you can convert one of your integers to strings.
int x = 1000000;
DecimalFormat formatter = new DecimalFormat("#,###,###");
String number_string = formatter.format(x);
System.out.println(number_string);
// Outputs 1,000,000
This JS function from Css Tricks - http://css-tricks.com/snippets/javascript/comma-values-in-numbers/
function CommaFormatted(amount) {
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3) {
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}

android pass a number through onclicklistener

I am trying to ask a user for 2 numbers x and y. When the user clicks the button it will subtract y from x and give the total. What I would like it to do is save the total, and the next time you click the button, it will subtract y from the total. I know I am not passing the total back through again, but I am not sure where to begin.
subtract.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) throws NumberFormatException {
if (v == subtract)
{
NumberFormat currencyFormatter;
currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
String bString;
String x = startingmoney.getText().toString();
String y = submoney.getText().toString ();
double total = 0.00;
double xm = 0.00;
double ym =0.00;
try
{
xm = Double.parseDouble(x);
}
catch(NumberFormatException n)
{
xm = 0.00;
}
try
{
ym = Double.parseDouble(y);
}
catch(NumberFormatException n)
{
ym = 0.00;
}
total = xm -ym;
bString = currencyFormatter.format(total);
endmoney.setText(bString);
tracker.setText("you have entered " + bString +"\n" + tracker.getText().toString());
There are many ways to do this. One possible way is to make the variable "x" and "y" static. This may not meet your requirement, since you may change the value of "x" and "y" dynamically.
Another way is to change the implement of OnClickListener. You can use it as,
class MyOnClickListener implements OnClickListener
and pass the class (the class which calls "setOnClickListener") to MyOnClickListener, that is, the constructor of MyOnClickListener is like,
MyOnClickListener(CLASS clazz){
this.clazz = clazz;
}
And you can do subtraction with clazz.getX() and clazz.getY().

Categories

Resources