I'm trying to reformat a string in java and remove the final decimal out of an android version number, so 4.2.2 would be formatted to 4.2.
Would I use REGEX to get the desired format for the android version number?
Here's my code for finding the android version number:
String osRelease = Build.VERSION.RELEASE;
http://i.imgur.com/UHRohGU.jpg [Error Log File From Eclipse]
Just use some String manipulation:
String i = "4.2.2";
String s = i.substring(0, Math.min(s.length(), 3));
System.out.println(s);
This will give you the first 3 characters (ie. "4.2") of your input String).
If you have your value as a Double or similar, use String.valueOf(myDouble) to convert to String.
Math.min() trick is from here.
Related
While calling this function item.getSymbol(), it returns data in this format.
ITC22JAN200PE
HINDUNILVR22JAN2300PE
ASIANPAINT22MAR2500PE
UI also shows same format in android.
holder.binding.symbolNameTextView.setText(item.getSymbol());
I want to show symbols in UI like this.
ITC 22JAN 200PE
HINDUNILVR 22JAN 2300PE
ASIANPAINT 22MAR 2500PE
How to add spaces after stock name and expiry?
Regex seems the way to go:
final String pattern = "(^\\D+)(\\d+(?:JAN|FEB|MAR|APR|JUN|JUL|AUG|SEP|OCT|NOV|DEC))(.*$)";
final String spaced = item.getSymbol().replaceAll(pattern, "$1 $2 $3");
holder.binding.symbolNameTextView.setText(spaced);
I want to display some coördinates in my Android app.
I receive two double values (lat and long in decimal degrees notation) from my backend.
Example data:
var lat : Double = 52.5027
var long : Double = 5.41982
Now i fill my textView in my class file:
tvLat.text = getString(R.string.gps_format, lat)
tvLong.text = getString(R.string.gps_format, lat)
and my xml resource file contains this:
<string name="gps_format">%,.5f°</string>
It displays:
52,50270°
5,41982°
As I live in the Netherlands it is formatted accordingly.
See https://developer.android.com/reference/java/util/Formatter
Number Localization Algorithm
...
If a decimal separator is present, a locale-specific decimal separator is substituted.
We typically use a , as a decimal separator, and . as a group separator in the Netherlands.
Question:
Is there an international standard for the notation of coördinates in decimal degrees which requires the decimal seperator to be .?
If there is, or I just want to, how do I achieve this without changing my locale or formatting for the entire app?
Desired output:
52.50270°
5.41982°
I have not found a way yet for the XML, so my guess is that it will have to be done with code. I've looked into the setDecimalSeparator method, but I can't get any working solution.
I've found something which could be called a dirty solution.
tvLat.text =
getString(R.string.gps_format,
DecimalFormat(
"0.00000",
DecimalFormatSymbols(Locale.getDefault())
.apply { decimalSeparator = '.' }
).format(lat).toString()
)
Along with a change in my resource file
<string name="gps_format">%s°</string>
New output:
52.50270°
5.41982°
I would like to have the format in my string resource though.
Anyone has suggestions?
When I run this line of code: Float.parseFloat("1460000 JPY") I get the error
Exception in thread "main" java.lang.NumberFormatException: For input string: "1460000 JPY"
This string is coming from an API call from a form where this is a text field with no validation. It usually works because people put in just a number, but sometimes you get this issue. How do I get it to return just the initial numbers as a float and disregard the trailing alpha characters?
You can use regex to find if that string contains only digit or not
String apistring = "1460000 JPY";
if(apistring.matches("[0-9]+")){
// do your code
}else{
// throw some error message
}
Stripping char from that will be difficult as you said its a input field and user can enter any text. You can strip it off only if you know that there is a particular pattern
Since DecimalFormat is very lenient about parsing Strings I would recommend that.
You can use it like this:
DecimalFormat df = new DecimalFormat();
try {
float parsedValue = df.parse("1460000 JPY").floatValue();
System.out.println(parsedValue);
} catch (ParseException pe) {
pe.printStackTrace();
// handle exception a bit more
}
This prints:
1460000.0
As you can see the parse method can throw a ParseException if the passed String starts with something else than a number, like:
blub 1460000 JPY
If that won't happen in your app, then you don't have to bother about it.
You can use regex to extract the numbers in input .
s = s.replaceAll("[^0-9]","");
and then parse float from it. Only downside is that it will extract all numbers (It will extract 1245 and 3 both from 1245 JPY 3).
UPDATE: to account for the bug that #Tom brought up:
Float.parseFloat("1.46 JPY".replaceAll("[^0-9.]",""));
1.46
the above is a superior solution. See below for explanation.
As #azurefrog said, stripping out non-numeric characters and then parsing what is left as a Float is the way to go.You can accomplish this using the following code:
Float.parseFloat("1460000 JPY".replaceAll("[^0-9]",""));
1460000.0
This is not very robust though, because for inputs like "1.46" the output becomes
146.0
.replaceAll("[^0-9.]","") fixes this inaccuracy by adding the decimal . character to the exclusion regex like so [^0-9.]
I have a legacy program (java 1.4) running under Tomcat/Jboss, however, i have copy it to a new server (java 1.7) and it throws the following exception.
java.lang.NumberFormatException: For input string: "0000000000000000009011,00"
sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1250)
java.lang.Double.valueOf(Double.java:504)
java.lang.Double.<init>(Double.java:597)
Since i don't have access to the source, is there any way to fix this? i haven't try with java 1.4 for the moment
You schould look to jvm parameters on old server. Your language parameters will change comma to dot actually so you do not need any code changes.
I think its not due to java version. Replace comma(,) with "" or period(.)
For example conside a string : String str = "0000000000000000009011,00";
Now you can try -
double result = Double.valueOf(str.replace(",", "."));
or
double result = Double.valueOf(str.replace(",", ""));
Rather use NumberFormat for parsing:
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("0000000000000000009011,00");
double d = number.doubleValue();
This way you can select the right locale for the number representation. In the example above I used France - they use a comma for the decimal point.
I am not posting any code I am struck with. I am trying this in Java:
Issue:
I have words like:
,xxxx-1223
yyyyy,xxdd-345
$,xxxxr-7
sdsdsdd-18
so what ever format I have I should be able to read the last one:
xxxx-1223
xxdd-345
xxxxr-7
sdsdsdd-18
what so may be the words, all I need to to get the words as shown.
Use String#lastIndexOf(int) to find where the last comma occurs, and use String#substring(int) to get the rest of the string that follows.
String input = /* whatever */;
int lastComma = input.lastIndexOf(',');
String output = input.substring(lastComma + 1);
String[] str=yourWord.split(",");
String output=str[str.length-1];
You can use this Regex: -
(\\w+-\\d+)$
Or this specific problem can simply be solved using String.split() or String.substring(int) methods