如何将所有非数字字符交换为货币格式

发布于 2024-11-05 08:17:14 字数 669 浏览 0 评论 0原文

我需要有人帮助,我将不胜感激,非常感谢

下面是左侧我想交换为货币格式/右侧的几个示例

  • 123,00 至 123.00
  • 1.123,00€ 至 1,123.00
  • $11.123,00 至 11,123.00
  • 12.123,00 ZH至 12,123.00
  • 12.123,00cH 至 12,123.00
  • £1.123.123,00 至 1,123,123.00
  • 112312300 KN 至 1,123,123.00

或例如

  • 1,123.00€ 至 1,123.00
  • $11.123.00 至11,123.00
  • 12,123,00 ZH 至 12,123.00
  • 12.123,00cH 至 12,123.00
  • £1.123,123,00 至 1,123,123.00
  • 11,231230.0 KN 至 1,123,123.00

这意味着左侧的任何内容应该更改为逗号作为千位数字和点作为十进制数字,

似乎可以使用 preg_replacestr_replace 来完成,但我应该做什么?

I need somebody help, I Would be appreciate it and many thanks

Below as several samples that left side i want to swap to currency format/right side

  • 123,00 to 123.00
  • 1.123,00€ to 1,123.00
  • $11.123,00 to 11,123.00
  • 12.123,00 ZH to 12,123.00
  • 12.123,00cH to 12,123.00
  • £1.123.123,00 to 1,123,123.00
  • 112312300 KN to 1,123,123.00

or e.g.

  • 1,123.00€ to 1,123.00
  • $11.123.00 to 11,123.00
  • 12,123,00 ZH to 12,123.00
  • 12.123,00cH to 12,123.00
  • £1.123,123,00 to 1,123,123.00
  • 11,231230.0 KN to 1,123,123.00

that mean whatever in left side it should be change to comma as thousand digit and dot as decimal digit,

it seem could be done using preg_replace or str_replace, but what I should do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

怀里藏娇 2024-11-12 08:17:14

去掉非数字字符,然后通过number_format()

$val = preg_replace("/[^0-9]/", "", "11,231230.0 KN") / 100;
$formatted = number_format($val, 2, ".", ",");

// Outputs 1,123,123.00

编辑添加了/ 100,因为它在原始零之后添加了小数位。

Srip out non-numeric characters, and then pass through number_format().

$val = preg_replace("/[^0-9]/", "", "11,231230.0 KN") / 100;
$formatted = number_format($val, 2, ".", ",");

// Outputs 1,123,123.00

EDIT added / 100 since it was adding the decimal places after the original zeros.

如梦 2024-11-12 08:17:14

最简单的方法是首先删除所有非数字内容,然后使用 <代码>数字格式

// assuming $str holds the original string
$str = preg_replace('/[^0-9]/', '', $str);  // now just a string of numbers
$str = number_format( $str, 2 ); // 2 decimal places

The easiest way to do this would be to first remove everything non-numeric and then use number_format.

// assuming $str holds the original string
$str = preg_replace('/[^0-9]/', '', $str);  // now just a string of numbers
$str = number_format( $str, 2 ); // 2 decimal places
給妳壹絲溫柔 2024-11-12 08:17:14

你可以尝试

print number_format(preg_replace('/\D/', '', $number), 2, '.', ',');

You could try

print number_format(preg_replace('/\D/', '', $number), 2, '.', ',');
云柯 2024-11-12 08:17:14

我必须对货币做同样的事情来规范我正在处理的项目的格式。

如果格式为 #####.## 并支持这些输入,代码将重新处理要输出的任何字符串:

// these are my unit test check
0.00 returned by filtering 0
0.20 returned by filtering 0.2
0.20 returned by filtering 0,2
0.22 returned by filtering 0.22
0.22 returned by filtering 0,22
18.20 returned by filtering 18.2
1000.00 returned by filtering 1 000
1000.00 returned by filtering 1,000
1000.00 returned by filtering 1,000.00
1000.00 returned by filtering 1000,00
1000.00 returned by filtering 1 000,00
1000.20 returned by filtering 1 000,2
1000.20 returned by filtering 1 000,20 $
1000.20 returned by filtering abc1def00test0dot.twenty20

以及与任何非数字字符的任何其他组合交错

    if (strpos($string, '.') === false
        && strpos($string, ',') === false
    ) {
        $string .= '.00';
    }

    $string = preg_replace('/[^0-9.,]/', '', $string);

    $string = preg_replace('/[.,]/', '.', $string);

    while (
        strpos($string, '.') != strrpos($string, '.')
        || strlen($string) - 1 - strrpos($string, '.') > 2
    ) {
        $pos = strpos($string, '.');
        if ($pos > 0) {
            $string = substr($string, 0, $pos) . substr($string, $pos +1);
        } else {
            break;
        }
    }

    return (float)$string;

最后使用 number_format() 重新格式化标准化数字

I have had to do the same from currency to normalize the format for a project i'm working on.

The code is reworking any string to output if formatted as #####.## and support these inputs :

// these are my unit test check
0.00 returned by filtering 0
0.20 returned by filtering 0.2
0.20 returned by filtering 0,2
0.22 returned by filtering 0.22
0.22 returned by filtering 0,22
18.20 returned by filtering 18.2
1000.00 returned by filtering 1 000
1000.00 returned by filtering 1,000
1000.00 returned by filtering 1,000.00
1000.00 returned by filtering 1000,00
1000.00 returned by filtering 1 000,00
1000.20 returned by filtering 1 000,2
1000.20 returned by filtering 1 000,20 $
1000.20 returned by filtering abc1def00test0dot.twenty20

and any other combination interlace with any non numeric chars

    if (strpos($string, '.') === false
        && strpos($string, ',') === false
    ) {
        $string .= '.00';
    }

    $string = preg_replace('/[^0-9.,]/', '', $string);

    $string = preg_replace('/[.,]/', '.', $string);

    while (
        strpos($string, '.') != strrpos($string, '.')
        || strlen($string) - 1 - strrpos($string, '.') > 2
    ) {
        $pos = strpos($string, '.');
        if ($pos > 0) {
            $string = substr($string, 0, $pos) . substr($string, $pos +1);
        } else {
            break;
        }
    }

    return (float)$string;

And finally use number_format() to reformat the standardized number

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