How do I convert decimal input into an int? - android

I have an EditText field where the user can enter his weight in Kilogram, i.e. 11.7. I want to store that in the database as gramms, i.e. 11700.
How do I convert the String "11.7" to the Integer 11700?

To get the value from an EditText use http://developer.android.com/reference/android/widget/EditText.html#getText()
String userInput = editText.getText().toString();
To convert a String into a double use the code from #govindpatel http://developer.android.com/reference/java/lang/Double.html#parseDouble(java.lang.String)
try {
double kg = Double.parseDouble(userInput)
}
catch (NumberFormatException nfe) {
editText.setError(R.string.invalid_double); // Or whatever you want to show the user
}
To convert from KG to g:
long gramms = Math.round(kg * 1000);
You could replace the double and long with float and int if you know you don't need the precision or range of the larger variables.

you have to parse it to int,long or double.
I think In your case you can do this
double grams = Double.parseDouble("11.7") * 1000;
double variable_name = Double.parseDouble(string_name) * number_you_want_to_multiply_with;
and this will give a double value. So you can convert that to integer, long or keep as it is in your db.
to convert double to long you can do something like this:
long value = (long)(grams);
Hope this helps you,

I'm not sure what it has to do with Android?
int grams = (int)(kg * 1000);
To get text from EditText
EditText mEdit = (EditText)findViewById(R.id.edittext);
double kg = Double.parseDouble(mEdit.getText().toString());

Related

How to get split string with special characters in android?

I have a string named namely "-10.00","-100.00","-1000.00". I want to get value like "10","100","1000" from that string. I have tried to get substring but did not able to get.
code i have tried
String amount = "-10.00";
String trimwalletBalance = amount.substring(0, amount.indexOf('.'));
From above i only get "-10".
String trimwalletBalance = amount.substring(1, amoun.indexOf("."));
Its very simple.
Do it like String trimwalletBalance = amount.substring(1, amount.indexOf('.'));
Instead of position 0, You should get substring from position 1
Convert it into integer and then name it positive:
String amount = "-10.00";
int amountInt = (int) Double.parseDouble(amount);
if(amountInt<0)amountInt*=-1;
Try
String amount = "-10.00";
int value = (int) Double.parseDouble(amount);
if(value < 0) value *= -1;
//value will be 10
OR
String text = amount.substring(1, amount.indexOf('.'));

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

number format exception invalid int

I have to add values to my sq lite from the list view. In my list view there are two edit texts and text view. I just want to get value from each edit text and multiply it to the corresponding text view value. when I am running the app, I need not to enter data to every edit text. due to this I am ending with a "number format exception : invalid int". From other examples I can understand that multiplication on null value may cause the error. how can I skip the null value contained edit texts from the iteration?
this is my code
protected void InsertDb() {
// TODO Auto-generated method stub
DatabaseHelper databasecontroller = new DatabaseHelper(Orders.this);
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();
if(list != null){
for(int i = 0; i< list.getChildCount();i++){
View vie = list.getChildAt(i);
EditText ed1= (EditText) vie.findViewById(R.id.cases);
EditText ed2 = (EditText) vie.findViewById(R.id.pcs);
String qty = ed1.getText().toString();
TextView tv = (TextView)findViewById(R.id.srp);
String imsrpv= tv.getText().toString();
float srps = Float.valueOf(imsrpv);
int qtys = Integer.valueOf(qty);
float amts = srps * qtys;
String amount = Float.toString(amts);
datanum.put("A",qty );
datanum.put("B",ed2.getText().toString() );
datanum.put("L", amount);
Log.d("value of amnt",amount);
databasecontroller.entercustdetails(datanum);
}
}
Log.v("compleated", data.toString());
}
Thanks in advance..
Check either returning value from edittext is a proper number or it is not null.if a null value is there it may give exeception of numberformat.you have to handle this.
if both are correct then you can also you
float srps=0.0;
int qtys=0;
try
{
srps = Float.valueOf(imsrpv);
qtys = Integer.valueOf(qty);
}
catch (NumberFormatException e)
{
srps =//Default value you want;
qtys =//Default value you want;
}
Hope it works..
Your problem is not how You have parsed the values. For explanation:
parseFloat(), parseInt() will return a primitive type of int and float and valueOf() returns a new type of Integer and Float. Also, parseInt(), parseFloat() will recognize plus and minus signs, valueOf() doesn´t. That´s the difference between these two types/methods. Your problem seems to be, that the value is empty and You can simply get rid of this by:
//set a default float and int
float srps = 0.0;
int qtys = 0;
//now parse the values by checking if the strings are not null or empty
if(imsrpv!=null&&!imsrpv.isEmpty()){
srps = Float.valueOf(imsrpv);
}
if(qty!=null&&!qty.isEmpty()){
qtys = Integer.valueOf(qty);
}
Like I said, if these values have a plus or minus sign and it is important for Your needs, You should use the parse method. Also, You don´t need a new Integer or Float type, so more correct is using parse() method.

Convert String to double

Is there way to convert a string to double. This is how i did.
String s = b.getText().toString();
double d = Double.parseDouble(s);
It gives NumberFormatException
Try this
String s = b.getText().toString();
double d = Double.valueOf(s.trim()).doubleValue();
You are probably starting off with a zero-length string. Check the length before you parse it. It also wouldn't hurt to catch NumberFormatException, although that shouldn't be problem if you have android:inputType="number" in the view's layout.
double d = 0;
CharSequence cs = b.getText();
if(cs != null && cs.length() > 0) {
d = Double.parseDouble(cs.toString());
}
i have a similar problem.
for correct formatting EditText text content to double value i use this code:
try {
String eAm = etAmount.getText().toString();
DecimalFormat dF = new DecimalFormat("0.00");
Number num = dF.parse(eAm);
mPayContext.amount = num.doubleValue();
} catch (Exception e) {
mPayContext.amount = 0.0d;
}
this is independet from current phone locale and return correct double value.
hope it's help;

Why does Integer.parseInt("53.6") fail?

I got an exception "Unable to parse 53.6 as an integer". What is the proper format?
double dbval;
double temp_val=12;
double p = 1.8;
double fvalue =p;
temp_val = temp_val * fvalue;
temp_val = (temp_val + 32);
String dbcal1=Double.toString(temp_val);
dbval = Integer.parseInt(dbcal1);
System.out.println("dbval"+dbval);
You can't parse 53.6 as an integer because it is not an integer.
If you are just trying to change temp_val to an integer there is no need to convert it to a string and back. You can cast use a cast instead:
int result = (int)temp_val;

Categories

Resources