开发者

Rounding using regular expressions

Is it possible to use a regular expression to perform rounding on a number? The problem is that I've got a lot of numbers to more than 2 decimal places in a file and I need to move them to 2 decimal places.

It's in source file so ideally I'd like to use Visual Studio's find and replace, but I'm not against the idea of writing a script to run on the file (treating it as plain text) if re开发者_开发知识库gex doesn't have the answer.


You can't do it with regexes alone, as Ryan pointed out, but you can use regexes to find decimals in your input and use a callback function on the replace operation.

In Python:

>>> import re
>>> text = "1.234 and 99.999; .555 12.345"
>>> simpledec = re.compile(r"\d*\.\d+")
>>> def mround(match):
...     return "{:.2f}".format(float(match.group()))
...
>>> re.sub(simpledec, mround, text)
'1.23 and 100.00; 0.56 12.35'

The regex to match decimals is very simplistic; it doesn't match exponential notation, for example, but it should give you an idea.


I don't think you can round the numbers, since regular expressions can't do addition.

You could truncate them as follows (this will work in Notepad++):

find (\d*\.\d\d)\d*

replace with \1


Well... at least you can search for (\d+\.\d\d\d)\d* and replace it with round($1, 2).

Yeah, perhaps this is not what you expected :)


I just came up with a simple way to do this for rounding results in a set of Google BigQuery results -- their subset of SQL doesn't let you round to a specific decimal place a.f.a.i.k., but it does support regexes.

Since, as someone mentioned above, regexes can't add, then as long you can add a constant to each value somehow, you can still get the rest of the way there:

+ 0.5 if you're rounding up to the nearest integer
+ 0.05 if you are rounding up to the nearest 0.1
+ 0.005 if you are rounding up to the nearest 0.01 etc.

... etc. - basically add half of your smallest precision value to get the rounding effect.

And then run a regex extract on the result. The above ones will probably work done for this.

Here's the regex pattern I settled on -- which is not the most robust, but will work on a number that is already known to be in basic float format:

+ for 0.1 precision use (-?\d*\.?\d?)
+ for 0.01 precision use (-?\d*\.?\d?\d?)
+ for 0.001 precision use (-?\d*\.?\d?\d?\d?)

Then it's both rounded and truncated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜