How to set thousands seperators with two decimal places? - android

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.

Related

Android Firebase how to use the spacial characters in the childs

I'm new in Firebase realtime database, and I have to save some special characters in the database like the following:
https://myProject.firebaseio.com/myProject/en/$fanny/weight
Note: the child could contain any special character not just only $
How could I handle such like this issue please?
As I see there are some guys are replacing the characters, but in my case I should not change the values, because I have to display it to the users as is because it has a meaningful to the user.
The only solution is to replace the invalid characters in the keys.
If you need to display the original value to the user, also store it as a value.

Calculator final output issue

I am creating a calculator app in android. It should calculate anonymously whatever is on textBox. For e.g. I enter 1+2.5-5*8 in textBox. But when I call addition method the app gets crashed. Because the input is in string format and answer I want is in numeric format. I used string buffer. I tried in java that when I enter (1+1+3-1) in stringBuffer and I display using println() method it give correct answer but same does not happen with string buffer when I take that value from editText.
You have to convert string into integer or float.
Use Integer.parseInt("") method to covert to integer and Float.parseFloat("") to convert to float.
First of all welcome to StackOverFlow
It might help if you add code and the error log in the question
So I am gonna cover all the possible things that might be happening wrong in your program:
NumberFormatException- you might not be converting the string "12" into int 12 to do so use Interger.parseInt(your string buffer with number only here);
Try using a stack and a queue for your solution (google it you will get details)
You might be making a mistake in getting string from edittext it sometimes give NullPointerException
if above is not enough comment and add your code asap

EditText converting decimal to exponential

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)))

Formatting fileds using android views

Is there any good simple way to format fields as I want.
I'd like to separate field value from field text. For example, I'd like to group number by 4, so when I set field's value to 123456789 it would show as 1 2345 6789, but when I get value I'll get number without spaces (123456789).
I think, this is the way widgets works on other tools, i.e. .Net VS. I can set there some mapping, formats and so on, so what is shown depends on value and format/mapping.
In android this must be done for passwords fields. It has text as value, but it shows some dots (value is mypassword and field shows **********).
I guess you just want to display the number in nice format, please use the APIs provided by PhoneNumberUtils.
static String formatNumber(String source)
static void formatNumber(Editable text, int defaultFormattingType)
You could set the formatted string to the text view for displaying nicely. But the saved content is still the raw content without any format.
Please refer to the SDK document at http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html

getting value 9.777123455E9 instead of 9777123455 from excel sheet

I am working on a project in which i am getting value from excel sheet(in assets android) and reflecting data in list view.
problem is:: phone no is not in proper format.
9.777123455E9 instead of 9777123455
When it's a phone number, you should always store the cell data as text, even if it consists only of digits, since a phone number is no mathematical number and when doing operations on it, you want to treat it as a string of characters (i.e. text).
If you input a phone number that looks to Excel like a mathematical number, it will interpret it as a number and in consequence will do things to it that make sense for numbers, but not necessarily for phone numbers, such as displaying it in scientific format.
To force Excel to treat your number as text, precede it with a single quote (apostrophe) when entering it. That is, enter into the cell:
'9777123455
It will be displayed without the single quote, just as you expect a phone number to be displayed and can be processed as text.
double d=9.777123455E9;
NumberFormat formatter = new DecimalFormat("#");
System.out.println(d);
System.out.println(formatter.format(d));
output
9.777123455E9
9777123455
E9 simply means multiply by 10^9
Update:
As #blubberdiblub mentioned, for phone numbers, it makes sense to change it to text. But for other cases, If you need to do mathematical operations leaving it in the scientific format works. You can right click on the column name and select formatting option to set the type of data the column will handle (number , text etc). If you want don't want to change the phone number to text and still see the number, simply increase the width of the column. The number will be shown full (without the "E").

Categories

Resources