繁体   English   中英

正则表达式以点/逗号检查/设置价格并解析为整数

[英]Regex to check/set a price with dot/comma and parse into a 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.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

信息:我的项目正在进行中。

如果可能的话,我想解决这个问题,因为我有一个不t 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的项目, t 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无论如何t 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粘贴它。

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+)?$

如果匹配,则将所有句点( . )替换为空白,并以逗号( , )分隔。

暂无
暂无

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

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