RegExp解析货币值 [英] RegExp to parse currency value

查看:108
本文介绍了RegExp解析货币值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在AS3中编写一个RegExp,它将一个Excel格式的货币值解析为一个数字: 例如. RegExp($ 35,600.00)= 35600

I would need to write a RegExp in AS3 which parses an Excel formatted currency value into a number: E.g. RegExp($35,600.00) = 35600

并检查其格式是否正确(以,"为千位分隔符,以."为小数点.货币符号可以是任何符号(不仅是$),可以位于开头或结尾处.结束.

And checks if it is correctly formatted (with the "," as the thousands separator, and the "." as the decimal point. The currency symbol could be any (not just $) and could stand at the beginning or the end.

所以我只需要从数字中去除每个非数字并检查是否有效.

So I would just need to strip every non digit from the number and check if is valid.

谢谢! 马丁

推荐答案

您将需要两种情况,一种是用逗号作为分隔符,另一种是用十进制分隔的整数.

You will need two cases, one for comma as separator and another for decimal separated whole numbers.

如果是整数,请删除逗号或小数点后的所有内容(取决于您的格式).然后运行以下正则表达式:

If a whole number, remove everything after the comma or decimal point(depending on your format). Then run the following Regex:

这将删除所有非数字字符:

This will remove all non-digit characters:

s/\D+//g;

如果没有整数,则需要为整数分隔符添加一个例外:

If you do not have a whole number, you will need to include an exception for the whole number separator:

小数点分隔符:

 s/[^\d.]+//g

逗号分隔符:

 s/[^\d,]+//g

*免责声明:我只在脑海中解析这些正则表达式,因此我的语法可能会略有偏离.

*Disclaimer: I only parsed these regexes in my head, so my syntax could be slightly off.

这篇关于RegExp解析货币值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆