Преобразование сумм, где ввод «100K», «100M»; так далее

В настоящее время я работаю на сайте, где мне нужно конвертировать суммы с буквами за ним, например:

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(); и другие функции, но это не работает, как я хочу это ..

Есть кто-нибудь, кто может мне помочь?

2

Решение

<?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);
}
}
3

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector