正则表达式用点/逗号检查/设置价格并解析为整数
我想接受价格并转换为整数,例如:
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;
}
I want to accept prices and convert to integers like:
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
Information: My project is in groovy.
If possible I would like to solve this problem because I've got a project that doesnt use Double to deal with prices, it converts to Integers after treat the String just like that but in javascript, I understand some parts of the code but I don't know how to implement in groovy/java, but I
ll paste it anyway.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先对照此正则表达式进行检查:
^(\d?\d?\d?\.?)*(,\d+)?$
如果匹配,则替换所有句点 (
.
) 为空白,并以逗号 (,
) 分隔。Check it against this regex first:
^(\d?\d?\d?\.?)*(,\d+)?$
And if it matches, then replace all the periods (
.
) to blank, and split at the comma (,
).