读取货币格式

发布于 2024-10-10 14:33:16 字数 1621 浏览 5 评论 0原文

也许对某些人来说这很容易,但我想学习

有两种货币格式:

第一种货币格式是1,123,123.12,这种格式可能类似于$1,123,123.121,123,123.12 €

第二种货币格式为 1.123.123,12,这可能是 $1.123.123,121.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.121,123,123.12€

第二种货币格式 < em>1.123.123,12 这可能是 $1.123.123,121.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 code
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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

前事休说 2024-10-17 14:33:16

检查最后 3 个字符是什么,是“,”还是“.”。用它来确定您的货币格式。

$value =  "1,123,123.12";
if($value[strlen($value)-3]==',') $currency_format = "A";
else $currency_format="B";

Check what the 3 last character is, if it is a "," or a ".". Use this to determine your currency format.

$value =  "1,123,123.12";
if($value[strlen($value)-3]==',') $currency_format = "A";
else $currency_format="B";
你丑哭了我 2024-10-17 14:33:16

假设您只使用美元和欧元(或英镑或任何其他具有两位小数的货币),最简单的解决方案是:

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
}

否则,您将必须设置一个循环来逐一迭代字符,或者确定根据货币返回的位数。

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.

ら栖息 2024-10-17 14:33:16
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 );
}

如果有 2 位小数,我们会去掉所有非数字字符,然后除以 100(从而替换小数)。
如果没有 2 位小数,我们就去掉所有非数字字符。

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).
If there are not 2 decimals, we just strip all non-digit characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文