我有程序我试图通过简单地将数字截断到2位小数来清理。我并不担心正确的舍入。这将是一个子程序添加到更大的"清理" VB脚本我一直致力于特定代码检查机器的使用。 (DMIS代码)
我一直在使用REGEX101,并且已经找到了许多方法来匹配我使用的信息。下面的内容记录了我需要保留的单个组中的所有信息
^GOTO\/(\w+,\d+\.\d+,\d+\.\d+,\d+\.\d+)
I' m开头的行看起来像这样
GOTO/CART,0.0025,-2.0533,1
我试图获得
GOTO/CART,0,-2.05,1
尾随小数(1.00)是可以接受的,但不是首选。另外,CART需要保持不变,使用\ w +命令(那里有两个可能的单词选择)这是REGEX能够做到的吗?
答案 0 :(得分:1)
此:
>> s = "GOTO/CART,0.0025,-2.0533,1"
>> Set r = New RegExp
>> r.Global = True
>> r.Pattern = "(\.\d{2})(\d+)"
>> WScript.Echo r.Replace(s, "$1")
>>
GOTO/CART,0.00,-2.05,1
>>
'删除第二个小数位后的数字'。
答案 1 :(得分:1)
没有正则表达式,舍入数字:
mystr = "GOTO/CART,0.0025,-2.0533,1"
myarr = split(mystr,",")
outstr = myarr(0) & "," & round(myarr(1),2) & "," & round(myarr(2),2) & "," & round(myarr(3),2)
按照正则表达式,删除第二个后的数字,然后删除.00
:
(?=\.00)\.\d+|(?!\.00)(\.\d\d)\d+
//replace by $1
//"GOTO/CART,1.0025,-2.0533,1.0010" -> "GOTO/CART,1,-2.05,1"