62,623
社区成员
发帖
与我相关
我的任务
分享import java.util.Scanner;
public class TestOutStream
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
String num = s.nextLine();
boolean b = num.matches("\\d+[\\.]?[\\d]+(,{1}[\\d]{2})|\\d*");
System.out.println(b);
if (b)
{
num = num.replaceAll("\\.", "").replaceAll(",", "\\.");
System.out.println(num);
}
}
}
String[] data = {"19,99", "1.999,99", "12,999", "122.999", "20,00.99"}; //测试数据
String regex = "\\d+[.]?\\d*([,]\\d{2})?"; //正则
for (String s : data) {
if (s.matches(regex)) {
System.out.printf("[%s] is ok. ", s);
s = s.replace(".", "").replace(",", ".");
System.out.printf("after replace, [%s]\n", s);
} else {
System.out.printf("[%s] is not ok.\n", s);
}
}