简体   繁体   English

Java中的正则表达式以查找字符串是否为带有尾随零的十进制数字格式

[英]Regex in java to find if a String is in the format of a decimal number with a trailing zero

I am reading a list of Strings, some of which are in the form of decimals and comapring them against another. 我正在阅读一个字符串列表,其中一些采用小数形式,并将它们与另一个字符串共同映射。 Sadly, in the first list, I find that all decimal terms aka all the strings have an extra trailing zero. 可悲的是,在第一个列表中,我发现所有十进制术语(即所有字符串)都有一个额外的尾随零。 Thus, my .equals() function or .contains() function does not work. 因此,我的.equals()函数或.contains()函数不起作用。

List 1 : 134.20, 16, abacus, 44.10, 12/2/2013, 4, 45.220
List 2 : 134.2,  16, abacus, 44.2,  12/2/2013, 4, 45.22

How do I add a trailing zero in List 2? 如何在清单2中添加尾随零? Is there way to use regex or so to somehow remove the trailing zero if present? 是否可以使用正则表达式等以某种方式删除尾随零(如果存在)?

Conversely, can I remove the trailing zero in List 1? 相反,我可以删除列表1中的尾随零吗?

To remove trailing zeroes from decimals: 要从小数删除尾随零:

String zeroless = str.replaceAll("(\\b\\d+\\.[1-9]*)0+\\b", "$1");

To add trailing a zero where there isn't one: 要在没有零的位置添加尾随零:

String zeroless = str.replaceAll("(\\b\\d+\\.\\d*)[1-9]\\b", "$10");

An important part of this regex is the wrapping with \\\\b - a "word boundary". 此正则表达式的重要部分是用\\\\b “单词边界”进行换行。


I'm not sure how useful either of these will be. 我不确定这两个都有用。 If you're try to see if a particular decimal is in a string, remove the trailing zeroes from it, then search for it in your input with optional trailing zeros, like this: 如果您尝试查看字符串中是否包含特定的十进制数,请从中删除尾随零,然后在输入中使用可选的尾随零进行搜索,如下所示:

String num;  // eg 123.456
if (input.matches(".*\\b" + num.replace(".", "\\.").replaceAll("0+$", "") + "0*\\b.*")) {
    // input contains the number
}

This will get read of all unneccessary trailing zeroes: 这将读取所有不必要的尾随零:

str = str.replaceAll("(\\d+\\.\\d+?)0+", "$1");

Examples: 例子:

123.456000 --> 123.456
123.000    --> 123.0

Update: In the comment you asked how to unconditionally append a zero. 更新:在注释中,您询问了如何无条件附加零。 This is how you do it: 这是您的操作方式:

str = str.replaceAll("(\\d+\\.\\d+)", "$10");

You could use String.format() to get them all to how ever many decimal places. 您可以使用String.format()将它们全部取到多少个小数位。

String yourValue = "4.4";
int range = 3;
String zeros = String.format("%."+range+"f", Float.parseFloat(yourValue)); // 3 decimal places
System.out.println(zeros); // prints 4.400

Change range as needed for different number of decimal places needed. 根据需要更改范围,以使所需的小数位数不同。

Alternatively, if you are just using them to check if they are equal/greater/less then why don't you just parse the string as Double (or other suitable object) and then compare the Doubles then "2.2" == "2.20" anyway and you can use the already defined equals() and compareTo() for those objects 另外,如果您只是使用它们来检查它们是否相等/更大/更少,那么为什么不将字符串解析为Double(或其他合适的对象),然后将Doubles进行比较,然后比较"2.2" == "2.20"无论如何,您可以为这些对象使用已经定义的equals()compareTo()

You should be using BigDecimal to compare decimal values, not Strings at all. 您应该使用BigDecimal来比较十进制值,而不是字符串。 Get rid of the regular expression, use new BigDecimal(String), set the scale you want, and then use compareTo(). 摆脱正则表达式,使用新的BigDecimal(String),设置所需的小数compareTo(). ,然后使用compareTo().

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM