I do have a code that is responsible for replacing all String decimal values with comma separator into String decimal value with point separator, but not all cases are covered.
value.replaceAll(",(?=[0-9]+,)", "").replaceAll(",", ".")
52,52d => 52.52d
65.00f => 65.00f
123,50 => 123.50
1,234,567,89 => 1234567.89
1,234,567.89 => 1234.567.89 (BAD), should be 1234567.89
You might first replace all the last comma's with a dot, and then replace the rest of the comma's with an empty string.
,(?=\d+[df]?(?!\S))
,
Match literally(?=
Positive lookahead, assert what is directly to the right is
\d+[df]?
Match 1+ digits and optional d
or f
(?!\S)
Assert a whitspace boundary to the right)
Close lookaheadExample code:
String s = "52,52d\n"
+ "65.00f\n"
+ "123,50\n"
+ "1,234,567,89\n"
+ "1,234,567.89";
System.out.println(s
.replaceAll(",(?=\\d+[df]?(?!\\S))", ".")
.replaceAll(",", "")
);
Output
52.52d
65.00f
123.50
1234567.89
1234567.89
See a Java demo
If there is more text that can contain a comma and you don't want to replace those as a side effect, you can also first match all the parts where you want to do the replacements on and for those matches do the replacements.
\b\d+(?:\,\d+)*(?:\.\d+)?[fd]?\b
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments