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").
Related
I'm developing sms APP and want to receive sms from the specific numbers. But number can be changed sometime with country code as +923201234567 or sometime without country code 03201234567 how I can compare number from database? because don't know in which format number is saved in database(with country code or without country code)
public boolean isMember(String phone, long id){
String query = "SELECT * from members where phone = ? AND active = 1 AND gid = ?";
Cursor c = dbActions.rawQuery(query, new String[]{String.valueOf(phone), String.valueOf(id)});
return c.moveToFirst();
}
Suppose if the number is saved in database without country code 03201234567 then my requirement is to get true if I compare it with country code. +923201234567. Country code could be changed.
PhoneNumberUtils.compare(); is not useful because it not compare with database.
If you can't acquire the correct information always; then you need to look into heuristics.
Meaning: you could write your own comparisons; and when you encounter two numbers like:
03201234567
+923201234567
you can figure: their "tail" is equal; the only difference is that the first one starts with 0 (so no country code) and the second one with +92. So it might be reasonable to declare those two numbers to be equal.
So a "solution" would do things like "normalize" your input (remove all non-digit content; except for leading + signs); and to then make such "tail-bound" comparisons.
If that is "too" fuzzy; I guess then you should step back and describe the requirement that you actually try to resolve here. Why are you comparing numbers; and what do you intend to do with the output of that comparison?!
Normalize all of the phone numbers into the same format before you put them into the database. That way you can just do a normal db search.
The other thing I've done for phone numbers is to convert all letters into the appropriate number, then remove all non digits, then just compare the last 7 digits.
Here is an example of what I'm trying to do:
If a phone number is entered as 6123044356 it will display as (612)304-4356. Should a guest hit the backspace key 5 times instead of removing the last 4 numbers entered and the dash, remove the last 5 numbers entered. The phone number at that point would be displayed as (612)30. Should the guest hit the backspace key 3 more times the last 3 numbers displayed would be removed, not the parenthesis. The phone number would then be displayed as 61.
Any suggestions on how to do this?
Look at libphonenumber library. It has AsYouTypeFormatter class in it, which will handle this for you.
Alternatively you can use PhoneNumberFormattingTextWatcher which basically does the same thing. You would use it like so:
editText.addTextChangeListener(new PhoneNumberFormattingTextWatcher());
I would do it just like the dialer does it. That way is most intuitive.
Basically when they are typing, as soon as a number, say 1234567, gets to 7 digits, split it up like 123-4567 for the user. Then when it reaches 10 digits, 123-4567890, split it up like (123)456-7890 for them. When they reach 11 digits, change the format to 1-(234)567-8901.
When they are removing numbers from an 11 digit number, as soon as they hit 10 digits, change back to (123)456-7890. After that, as soon as the number of digits is less than 10, change the format back to 123-456789. Keep it in that format until they reach six digits when you just change it to 123456.
By changing the numbers to and from recognizable forms when a user reaches a certain number of digits, it subtly alerts the user that they have either created a real number, or there are too many or not enough digits for this to be a real number.
I suggest using the following thresholds:
11 digits: 1-(234)567-8901
10 digits: (123)456-7890 (anything less than this looks like 7 digits with digits on the end)
7 digits: 123-4567 (anything less just removes the dash).
I hope that makes sense. I do not think the user should have to input/erase any extra characters like parentheses and dashes. It should all be taken care of by the app.
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
I am writing a dictionary-type app. I have a list of hash-mapped terms and definitions. The basic premise is that there is a list of words that you tap on to see the definitions.
I have this functionality up and running - I am now trying to put dynamic links between the definitions.
Example: say the user taps on an item in the list, "dog". The definition might pop up, saying "A small furry [animal], commonly kept as a pet. See also [cat].". The intention is that the user can click on the word [animal] or [cat] and go to the appropriate definition. I've already gone to the trouble of making sure that any links in definitions are bounded by square brackets, so it's just a case of scanning the pop-up string for text [surrounded by brackets] and providing a link to that definition.
Note that definitions can contain multiple links, whilst some don't contain any links.
I have access to the string before it is displayed, so I guess the best way to do this is to do the scanning and ready the links before the dialog box is displayed.
The question is, how would I go about scanning for text surrounded by square brackets, and returning the text contained within those brackets?
Ideally the actual dialog box that is displayed would be devoid of the square brackets, and I need to also figure out a way of putting hyperlinks into a dialog box's text, but I'll cross that bridge when I come to it.
I'm new to Java - I've come from MATLAB and am just about staying afloat, but this is a less common task than I've had to deal with so far!
You could probably do this with a regular expression; something like this:
([^[]*)(\[[^]]+\])
which describes two "match groups"; the first of which means any string of zero or more characters that aren't "[" and the second of which means any string starting with "[", containing one or more characters that aren't "]", and ending with "]".
Then you could scan through your input for matches to this pattern. The first match group is passed through unchanged, and the second match group gets converted to a link. When the pattern stops matching your input, take whatever's left over and transmit that unchanged as well.
You'll have to experiment a little; regular expressions typically take some debugging. If your link text can only contain alphanumerics and spaces, your pattern would look more like this:
([^[]*)(\[[\s\w]+\])
Also, you may find that regular expression matching under Android is too slow to be practical, in which case you'll have to use wasyl's suggestion.
Quite simple, I think... As the text is in brackets, you need to scan every letter. So the basic recipe would be :
in a while loop scan every character (let's say, while i < len(text))
If scanned character is [:
i++;
Add letter at index i to some temporary variable
while (character # i) != ']' append it to the temporary variable
store this temporary variable in a list of results.
Some tips:
If you use solution above, use StringBuilder to append text (as regular string is immutable)
You might also want (and it's better, I think) to store starting and ending positions of all square brackets first, and then use string.substring() on each pair to get the text inside. This way you'd first iterate definition to find brackets (maybe catch unmatched ones, for early error handling), then iterate pairs of indices...
As for links, maybe this will be of use: How can I get clickable hyperlinks in AlertDialog from a string resource?
I'm looking for a way to compare 2 strings partial. I need to clear this with an example.
The base string is "equality".
The string I need to check is spelled wrong: "equallaty". I want to conform this is partially correct so the input, even not right in a grammar way, is the same as the base string.
Now I can of course parse the string to an char array. Now I can check every single character, but if I check the first 4 characters they will be right, the rest will be wrong even if there are only 2 mistakes. So the check I want to use is that a minimum of 70 procent of the characters should match.
Is anyone able to help me get on the right track?
Compare the strings with an edit-distance metric like the Levenshtein distance. Such a metric basically counts the number of changes needed to make the strings equal. If the number of changes is small relative to the total size of the string, then you can consider the strings similar.