Reputation: 11
I've got programs I'm trying to clean up by simply truncating the number to 2 decimal places. I'm not worried about correct rounding. This will be a subroutine added into a larger "Cleanup" VB script I've been working on for the specific code inspection machines use. (DMIS code)
I've been using REGEX101 and gotten as far as find a number of ways to match the information I'm using. The one below captures all the information I need to keep from the line in a single group
^GOTO\/(\w+,\d+\.\d+,\d+\.\d+,\d+\.\d+)
The lines I'm starting with look like this
GOTO/CART,0.0025,-2.0533,1
and I'm trying to get
GOTO/CART,0,-2.05,1
Trailing decimals (1.00) would be acceptable, but not preferred. ALso, the CART, needs to be kept unchageds, using a \w+ command (there are two possible word choices there) Is this something REGEX is capable of?
Upvotes: 1
Views: 620
Reputation: 2855
Without regex, rounding numbers:
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)
By Regex, remove digits after 2nd one, and removes .00
:
(?=\.00)\.\d+|(?!\.00)(\.\d\d)\d+
//replace by $1
//"GOTO/CART,1.0025,-2.0533,1.0010" -> "GOTO/CART,1,-2.05,1"
Upvotes: 1
Reputation: 38775
This:
>> 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
>>
'remove(s) digits after second decimal place'.
Upvotes: 1