简体   繁体   English

阅读货币格式

[英]read the currency format

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 第一种货币格式是1,123,123.12 ,这种格式可能是1,123,123.12 美元1,123,123.12€

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 第二种货币格式是1.123.123,12 ,这可能是$ 1.123.123,121.123.123,12€所以区别在于点和逗号的位置

The above format will be 以上格式将是

$this->value('one of the currency format insert here');

eg $this->value('$1,123,123.12'); 例如$this->value('$1,123,123.12'); or $this->value('1.123.123,12€'); $this->value('1.123.123,12€');

that I want to know is the code if (first currency format) {use blah .. blah ..} elseif (second currency format) {use blah .. blah ..} else {/ / unsupported format} 我想知道的是代码if (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 然后我改变了我的prevoious部分,以便可以使用$ value ['amount']

first currency format 1,123,123.12 and this format may be like $1,123,123.12 or 1,123,123.12€ and 第一种货币格式1,123,123.12 ,此格式可能类似于$ 1,123,123.121,123,123.12€

second currency format 1.123.123,12 and this may be such $1.123.123,12 or 1.123.123,12€ 第二种货币格式1.123.123,12 ,这可能是1.123.123,121.123.123,12€

then the value['amount'] will identify first with code like the following conditional 那么值['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..

at the end output as look like: 在结尾输出如下:

$identify = new curr_format();
echo $identify->curr_format($value['amount'],$else_statement);

Check what the 3 last character is, if it is a "," or a ".". 检查3个最后一个字符是什么,如果它是“,”或“。”。 Use this to determine your currency format. 使用此选项可确定您的货币格式。

$value =  "1,123,123.12";
if($value[strlen($value)-3]==',') $currency_format = "A";
else $currency_format="B";
function getNumber( $inStr ){
  if( preg_match( '/[\,\.]\d{2}\D?$/' , $inStr ) ){
   # Has 2 Decimal Places
    return (float) preg_replace( '/\D/' , '' , $inStr )/100;
  }
  return (int) preg_replace( '/\D/' , '' , $inStr );
}

In cases where there are 2 decimals, we strip all non-digit characters, and then divide by 100 (thereby replacing the decimals). 如果有2位小数,我们将删除所有非数字字符,然后除以100(从而替换小数)。 If there are not 2 decimals, we just strip all non-digit characters. 如果没有2位小数,我们只删除所有非数字字符。

Assuming you're only working with dollars and euros (or pounds or anything else that has two decimal digits, the easiest solution is: 假设你只使用美元和欧元(或磅或其他任何有两位小数的数字,最简单的解决方案是:

var value = str_replace('€','',$this->value);

if (substr($value,str_len($value)-3,1)=='.') {
  // first format
}
else if (substr($value,str_len($value)-3,1)==',') {
  // second format
}
else {
  // unsupported
}

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. 否则,您必须设置一个循环,逐个迭代字符,或根据货币确定返回的位数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM