开发者

how can I swap out all non numeric characters to currency format

开发者 https://www.devze.com 2023-03-02 23:11 出处:网络
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

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?


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.


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


You could try

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消