patternjavaModerate
Find index of double number delimiter
Viewed 0 times
numberdoublefindindexdelimiter
Problem
I have a
I've come to two options:
Second option with the same logic but different style:
I do not need to have a
Is there a cleaner way to achieve this goal? And which of these styles are better, in your opinion? Is there some way to use the
String with a double number. Unfortunately, the number is created on backends with different locals, so it could be both 101.02 and 101,02 (different delimiters). I need to get the position of this delimiter if it exists and get 0, if it is not.I've come to two options:
int pos = amount.indexOf(',') == -1 ?
(amount.indexOf('.') == -1 ? 0 : amount.indexOf('.'))
: amount.indexOf(',');Second option with the same logic but different style:
int pos = amount.indexOf(',');
if (pos == -1) pos = amount.indexOf('.');
if (pos == -1) pos = 0;I do not need to have a
double number from String, I just need the position of the delimiter to color the String (using the Android class Spannable).Is there a cleaner way to achieve this goal? And which of these styles are better, in your opinion? Is there some way to use the
DecimalFormat class to achieve the goal?Solution
The problems with the first approach
are that:
As such, the second approach
is the most preferable between the two, mainly for clarity. You should consider putting that into a utility method.
There would be other approaches like using a regular expression, but another good one would to not traverse the string potentially 2 times, and instead of looking whether the string has a certain character, loop through each character and see if it is one of the potential delimiters. With Java 8, you could have
And you could write explicitly the
Final point: having a position of 0 when neither
int pos = amount.indexOf(',') == -1 ?
(amount.indexOf('.') == -1 ? 0 : amount.indexOf('.'))
: amount.indexOf(',');are that:
- It's not that easily readable; the ternary operator has value when what is tested is simple enough. But when you start nesting them, it often degrades clarity.
- The index is calculated two times, one to test whether it is -1 or not, and the second time to return the value.
As such, the second approach
int pos = amount.indexOf(',');
if (pos == -1) pos = amount.indexOf('.');
if (pos == -1) pos = 0;is the most preferable between the two, mainly for clarity. You should consider putting that into a utility method.
There would be other approaches like using a regular expression, but another good one would to not traverse the string potentially 2 times, and instead of looking whether the string has a certain character, loop through each character and see if it is one of the potential delimiters. With Java 8, you could have
int pos = amount.chars().filter(c -> c == '.' || c == ',').findFirst().orElse(0);And you could write explicitly the
for loop for Java ≤ 7.Final point: having a position of 0 when neither
, nor . are present in the String can be confusing; 0 is a valid index value for a string, and it can imply that the delimiter was the first character of the string. If you consider ".25" (that could be a valid representation of a double number, the 0 before being implied), the code would consider this as if having no delimiter.Code Snippets
int pos = amount.indexOf(',') == -1 ?
(amount.indexOf('.') == -1 ? 0 : amount.indexOf('.'))
: amount.indexOf(',');int pos = amount.indexOf(',');
if (pos == -1) pos = amount.indexOf('.');
if (pos == -1) pos = 0;int pos = amount.chars().filter(c -> c == '.' || c == ',').findFirst().orElse(0);Context
StackExchange Code Review Q#148627, answer score: 10
Revisions (0)
No revisions yet.