I am storing a value in database as 0.0004 and displaying the same in EditText but in edittext it as showing as 4.0E-4 tried a lot of things nothing seems to work for me.
Storing value in database is fine but when coming to edittext display it is wrong.
What I have tried:
Increased the size of edit text but didn't solve the problem.
Followed below process
DecimalFormat FORMATTER = new DecimalFormat("0.####");
textvalue.setText(REAL_FORMATTER.format(new_percent.getLong(new_percent.getColumnIndex(bt.column2))));
but the result is same no chage.
Can any one help to solve this..I want the display in edit text as 0.0004 not as 4.0E-4
Thanks for your time.
Instead of giving pattern to the BigDecimal just send directly the value to the constructor parameter.
example:
BigDecimal number = new BigDecimal("4.0E-4");
int ints= number.intValue(); //BigDecimal to integer
double doubles= number.doubleValue(); //BigDecimal to double
Why you retrieve 0.0004 value with the getLong mehod?
As I suppose, you store the 0.0004 in a database in a REAL column type,
so you should retrieve this with getDouble.
DecimalFormat formatter = new DecimalFormat("0.####");
textValue.setText(
formatter.format(
new_percent.getDouble(
new_percent.getColumnIndexOrThrow(bt.column2))));
Finally achieved as below.
BigDecimal.valueOf(Cursor.getDouble(Cursor.getColumnIndex(bt.column2)))
Related
I cannot restrict the TextView to show only 3 decimals when I'm using a variable from another class. Here's the problem:
I have this TextView
public static TextView pt_resultado;
However when I tried to use it to show the number inputted from another screen while converting from Double restricting the decimals:
pt_resultado.setText(String.format("%.3f",(Double.toString(ActivityPopulacao.pt_resultado2))));
I get the following error:
java.util.IllegalFormatConversionException: %f can't format java.lang.String arguments
Is there a way to show only three decimals, put without changing the conversion?
You are trying inserting string as float.
You should do:
pt_resultado.setText(String.format("%.3f",Double.valueOf(ActivityPopulacao.pt_resultado2.getText().toString())))
You need to:
Retrieve String from TextView.
Convert String to Double/Float.
Format Double/Float value as String at 3 decimal places.
I assumed that ActivityPopulacao.pt_resultado2 is also TextView!
To help you also understand why this is happening ill break it flow a bit.
double recievedNum = ActivityPopulacao.pt_resultado2;
String convertedNumAsString = String.format ("%.3f", recievedNum);
pt_resultado.setText(convertedNumAsString);
Can you see the mistake? Your using Double.toString(number) where its expecting a number. String.format takes in the double does formatting and converts to string for you.
Defining TextView as a static view will cause memory leaks. Going forward, you can pass value from one activity to another using Intent - I presume you're using activity. Also, getText().toString() has already converted the input value to string. So you don't need that Double.toString() any more. Copy below code and paste it in the code that is responsible for starting the new activity.
Intent in = new Intent(ActivityPopulacao.this, NewActivity.class);
in.putExtra("key", pt_resultado2.getText().toString());
startActivity(in);
To receive the value, get the intent and request for the string in your NewActivity onCreate() method.
String newDouble = getIntent().getStringExtra("key");
pt_resultado.setText(newString);
Note that NewActivity in the above snippet refers to the activity that needs the value.
Why do you convert to string and try to format it like it is a Double?
The format() method expects a number to format.
Since ActivityPopulacao.pt_resultado2 is Double you should format the double value:
pt_resultado.setText(String.format("%.3f", ActivityPopulacao.pt_resultado2));
I can't seem to get my head around this. If have tried the following approaches:
DecimalFormat df = new DecimalFormat("00000.00");
DecimalFormat df = new DecimalFormat("######.##");
But the following line always generates and IllegalArgumentException.
double price = Double.valueOf(df.format(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString()));
// Sample input passed is value of 200 or some other whole number.
item.setPrice(price);
It doesn't make sense as I only copied the obvious solutions in this forum. Most of you got the format() to work.
Originally, I didn't have these lines of code. I just call my setPrice() method after getting the item price. This works. However, Double.valueOf() has a nasty habit of using only one decimal position.
e.g. passed 200. I get 200.0 inside my item object. I figured by using DecimalFormat I could've prevented this but it appears this caused me MORE headaches instead.
When you say you pass 200 and you get 200.0, you mean you get it in a double value? If so, that doesn't matter - it's a number and 200 = 200.0 for double values.
format(...) turns a double value to a String value. You have it the other way round. That's why you get the Exception.
If the price variable is actually a double you should do
double price = Double.valueOf(((EditText) view
.findViewById(R.id.edit_item_price)).getText().toString())
But I think you want that the price is a String, then you should convert the text from the EditText to a double and that double back to a String with something like new DecimalFormat("0.00")
I am getting a value from server that is not containing any Floating point let say its
1234 and have to cvonvert it in Floating value with 2 decimal point like 12.34.
Right now what i am doing is getting value storing it in float that convert the current value 1234 to 1234.0
after that doing this
tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("###.##");
RewardsBalance=df.format(tempB);
But with this i m having an issue that when i have value such that 1230 it results in 12.3 not that 12.30
but when i have value 1234 it gives the desired result that is 12.34
so what step i m missing any clue
12.3 and 12.30 are the same value. The problem is not the value but the code that incorrectly converts the right value to the wrong representation. You probably want "###.00". With "#", zero shows as absent.
Use this it will work
Two digits after point
Try this :
String.format("%.2f", your_value);
It will do just like you want
try this
tempB=Math.floor(tempB)/100.0;
DecimalFormat df = new DecimalFormat("0.00");
df.format(tempB);
it will work fine.
In my application ,am restricting the user to enter 7.2 digits(like 1234567.12).While entering it should be displayed as 1,234,567.12.But it has to be saved in database as 1234567.12.I am using decimal format as"###,###.##" to display in edit text box.But it is getting error on parsing with float ,while am saving in database.Please tell me any suggestions.
On Formatting ,am using this formatter _decimalFormat =new DecimalFormat("###,###,###.##");
_decimalFormat.setMinimumFractionDigits(2);
_decimalFormat.setMaximumFractionDigits(2);
in ontextChanged listener,On saving in database i am getting error in this line [amountText2.setText(Float.parseFloat(amountText1.getText().toString()));]
Remove the commas in your EditText's contained string and parse that into a float. From your code above, it should be something like:
amountText1.getText().toString().replace(",", "")
Here, you're replacing the commas with an empty string which is pretty much the same as removing them.
First of all please excuse me for my bad English speaking.
I am new to Android Development. I have a problem and think you can solve it.
The problem is:
I have a very big double value like 12345678987654321 in my android app
but when i want to show it on EditText, it will be shown like this 12345678987654300.
In this case when my value characters is over than 15 chars android shows remaining chars with "0"
i don't know what i have to to do.
i am using this code:
DecimalFormat df = new DecimalFormat("#.#########");
double a = Distancevals[1] * Distancevals[2];
//Distancevals is an array of double with big values
EditText editto = (EditText)findViewById(...);
editto.setText(df.format(a));
Double stores your number 12345678987654321 in format 1.23456789876543E16, so you lose the end of the number 21. When you format the result it's known that your number consists of 17 signs, so format function adds two zeros in the end of your number instead of 21.
Try to use this:
DecimalFormat df = new DecimalFormat("#.#########");
BigDecimal a = new BigDecimal(Distancevals[1] * Distancevals[2]);
// Distancevals is an array of double with big values
EditText editto = (EditText) findViewById(...);
editto.setText(df.format(a));
Try this one
DecimalFormat df= new DecimalFormat("###############00");
double a=Distancevals[1]*Distancevals[2];
//Distancevals is an array of double with big values
EditText editto=(EditText)findViewById(...);
editto.setText(df.format(a));
You just show it as editto.setText(a+"");
Cause in your case EditText is not doing anything but DecimalFormat is changing your number.
I suggest that in the line:
editto.setText(df.format(a));
Change it to:
editto.setText(df.format(a.toString()));