php - 转换输入为 "100K"、 "100M"等的金额

标签 php currency

我目前正在开发一个网站,需要转换后面带有字母的金额,例如:

100M = 100.000.000

我已经创建了一种以另一种方式执行此操作的方法,来自:

100.000.000 = 100M

这是我当前的功能:

function Convert($Input){

if($Input<=1000){
    $AmountCode = "GP";
    $Amount = $Input;
}
else if($Input>=1000000){
    $AmountCode = "M";
    $Amount = floatval($Input / 1000000);
}
else if($Input>=1000){
    $AmountCode = "K";
    $Amount = $Input / 1000;

}

$Array = array(
    'Amount' => $Amount,
    'Code' => $AmountCode
);

$Result = json_encode($Array);

return json_decode($Result);

}

现在我需要一些可以过滤掉这个的东西:

100GP = 100
100K  = 100.000
100M  = 100.000.000

我一直在寻找一些东西,并尝试使用 explode(); 和其他函数,但它并不像我想要的那样工作..

有谁可以帮我吗?

最佳答案

<?php
/**
  * @param string $input
  * @return integer
  */
function revert($input) {
    if (!is_string($input) || strlen(trim($input)) == 0) {
        throw new InvalidArgumentException('parameter input must be a string');
    }
    $amountCode = array ('GP'   => '',
                         'K'    => '000',
                         'M'    => '000000');
    $keys       = implode('|', array_keys($amountCode));
    $pattern    = '#[0-9]+(' . $keys .'){1,1}$#';
    $matches    = array();

    if (preg_match($pattern, $input, $matches)) {
        return $matches[1];
    }
    else {
        throw new Exception('can not revert this input: ' . $input);
    }
} 

关于php - 转换输入为 "100K"、 "100M"等的金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623315/

相关文章:

PHP - 上传到服务器时获取apk包名称

php - get_var 没有返回值

ruby-on-rails - 在 Ruby on Rails 中处理国际货币输入

java MonetaryConversions 为高位货币抛出 ArithmeticException

java - 如何将货币的欧元符号放在输出的末尾而不是开头?

php - 警告 : mysqli_fetch_array() expects parameter 1 to be mysqli_result when I have 2 parameters

php - 由于 echo 数组而没有 ajax 响应

javascript - 确认 JavaScript 后运行 php 代码

java - 为什么从Executors实用程序类返回的ExecutorService的execute()方法无法自然终止

android - 如何将 ISO4217 货币代码提供给 NumberFormat?