读取货币格式
也许对某些人来说这很容易,但我想学习
有两种货币格式:
第一种货币格式是1,123,123.12,这种格式可能类似于$1,123,123.12或1,123,123.12 €,
第二种货币格式为 1.123.123,12,这可能是 $1.123.123,12 或 1.123.123,12€< /strong> 所以区别在于点和逗号的位置
上面的格式将是
$this->value('one of the currency format insert here');
例如 $this->value('$1,123,123.12');
或 $this->value('1.123.123,12€');
我想知道的 是代码 if(第一种货币格式){use blah .. blah ..} elseif(第二种货币格式){use blah .. blah ..} else {//不支持的格式}
那么代码如何识别条目输入的是第一种货币格式还是第二种货币格式?
感谢您的指点和想法。
更新:
对于给出示例时的错误,我深表歉意
当我尝试测试代码时,我有点困惑,因为它似乎不起作用
,然后我更改了之前的部分,以便 $value['amount '] 它可能使用
第一种货币格式 1,123,123.12,这种格式可能类似于 $1,123,123.12 或 1,123,123.12€ 和
第二种货币格式 < em>1.123.123,12 这可能是 $1.123.123,12 或 1.123.123,12€
然后 value[' amount'] 将首先在最终输出中使用如下条件代码进行识别,
class curr_format {
private bla...bla..1
private bla...bla..2
var etc..
public function curr_format ($bla...,$and_bla..) {
//then make conditional is here
if (first currency format) {//use blah .. blah ..}
elseif (second currency format) {//use blah .. blah ..}
else {/ / unsupported format}
//another codes..
如下所示:
$identify = new curr_format();
echo $identify->curr_format($value['amount'],$else_statement);
perhaps for some people this is easy, but I want to learn
There are 2 currency formats:
first currency format is 1,123,123.12 and this format may be like $1,123,123.12 or 1,123,123.12€ and
second currency format is 1.123.123,12 and this may be such $1.123.123,12 or 1.123.123,12€ so the difference is the placement of dots and commas
The above format will be
$this->value('one of the currency format insert here');
e.g. $this->value('$1,123,123.12');
or $this->value('1.123.123,12€');
that I want to know is the codeif (first currency format) {use blah .. blah ..} elseif (second currency format) {use blah .. blah ..} else {/ / unsupported format}
so how the code to identify whether the entry is input the first currency format or second currency format?
thanks for the your pointers and ideas.
UPDATED:
I apologize for mistake in giving examples
When I tried to test the code I have a little confused because it seems not working
then I changed my prevoious parts, so that $value['amount'] it may be use
first currency format 1,123,123.12 and this format may be like $1,123,123.12 or 1,123,123.12€ and
second currency format 1.123.123,12 and this may be such $1.123.123,12 or 1.123.123,12€
then the value['amount'] will identify first with code like the following conditional
class curr_format {
private bla...bla..1
private bla...bla..2
var etc..
public function curr_format ($bla...,$and_bla..) {
//then make conditional is here
if (first currency format) {//use blah .. blah ..}
elseif (second currency format) {//use blah .. blah ..}
else {/ / unsupported format}
//another codes..
at the end output as look like:
$identify = new curr_format();
echo $identify->curr_format($value['amount'],$else_statement);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查最后 3 个字符是什么,是“,”还是“.”。用它来确定您的货币格式。
Check what the 3 last character is, if it is a "," or a ".". Use this to determine your currency format.
假设您只使用美元和欧元(或英镑或任何其他具有两位小数的货币),最简单的解决方案是:
否则,您将必须设置一个循环来逐一迭代字符,或者确定根据货币返回的位数。
Assuming you're only working with dollars and euros (or pounds or anything else that has two decimal digits, the easiest solution is:
Otherwise, you would have to set up a loop which iterates back through the characters one by one, or determines the number of digits back based on the currency.
如果有 2 位小数,我们会去掉所有非数字字符,然后除以 100(从而替换小数)。
如果没有 2 位小数,我们就去掉所有非数字字符。
In cases where there are 2 decimals, we strip all non-digit characters, and then divide by 100 (thereby replacing the decimals).
If there are not 2 decimals, we just strip all non-digit characters.