Java Regex使用点/逗号检查/设置价格并解析为整数
我想接受价格并转换为整数,如:Java Regex使用点/逗号检查/设置价格并解析为整数,java,javascript,regex,groovy,integer,Java,Javascript,Regex,Groovy,Integer,我想接受价格并转换为整数,如: 1.400 -> true, "$1.400,00" , 140000 1.400,00 -> true, "$1.400,00" , 140000 1.400,0 or 1.400,000000 -> true, "$1.400,00" , 140000 //get only the first 2 digits before the comma ->1.400,9394239 -> 1.400,93
1.400 -> true, "$1.400,00" , 140000
1.400,00 -> true, "$1.400,00" , 140000
1.400,0 or 1.400,000000 -> true, "$1.400,00" , 140000
//get only the first 2 digits before the comma ->1.400,9394239 -> 1.400,93
1.400.000 -> true, "$1.400.000,00", 140000000
1.400.00 -> false
1.40.00 -> false
14,00 -> true, "$14,00" , 1400
14 -> true, "$14,00" , 1400
14.00 -> false
信息:我的项目使用groovy
如果可能的话,我想解决这个问题,因为我有一个项目,它不使用Double来处理价格,它在像那样处理字符串之后转换成整数,但在javascript中,我理解代码的某些部分,但我不知道如何在groovy/java中实现,但无论如何我会粘贴它
String.prototype.formatNumber = function() {
var value = this.replace(",",".").replace(/[^\d\.]/g,'');
value = 0+value;
while (isNaN(value))
value = value.substring(0,valor.length-1);
value = parseInt(value * 100) / 100;
return value;
}
首先对照此正则表达式进行检查:
^(\d?\d?\d?\)*(,\d+)$
如果匹配,则将所有句点(
)替换为空,并在逗号处拆分(,
)