Usuń bezużyteczne cyfry zerowe z miejsc dziesiętnych w PHP
Próbuję znaleźć szybki sposób na usunięcie zero decimals
z wartości liczbowych takich jak:
echo cleanNumber('125.00');
// 125
echo cleanNumber('966.70');
// 966.7
echo cleanNumber(844.011);
// 844.011
Czy istnieje jakiś zoptymalizowany sposób na to?
19 answers
$num + 0
wystarczy.
echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7
Wewnętrznie, jest to równoznaczne z odlewania do pływaka z (float)$num
lub floatval($num)
, ale uważam, że prostsze.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-26 09:35:59
Możesz po prostu użyć funkcji floatval
echo floatval('125.00');
// 125
echo floatval('966.70');
// 966.7
echo floatval('844.011');
// 844.011
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-25 23:05:52
Po prostu dodanie +
do zmiennej string spowoduje, że typecast zostanie zamieniony na (float) i usunie zera:
echo +'125.00'; // 125
echo +'966.70'; // 966.7
echo +844.011; // 844.011
var_dump(+'844.011asdf'); // double(844.011)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-07 14:05:12
Tego używam:
function TrimTrailingZeroes($nbr) {
return strpos($nbr,'.')!==false ? rtrim(rtrim($nbr,'0'),'.') : $nbr;
}
Uwaga: zakłada się, że .
jest separatorem dziesiętnym. Ma tę zaletę, że będzie działał na dowolnie dużych (lub małych) liczbach, ponieważ nie ma float cast. Nie zmienia też liczb w notację naukową (np. 1.0 E-17).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-17 19:06:10
Powinieneś rzucać swoje liczby jak pływaki, co zrobi to za Ciebie.
$string = "42.422005000000000000000000000000";
echo (float)$string;
Wyjście tego będzie tym, czego szukasz.
42.422005
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-25 23:07:49
$x = '100.10';
$x = preg_replace("/\.?0*$/",'',$x);
echo $x;
Nie ma nic, czego nie da się naprawić prostym regexem;)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-12 10:52:30
Dla wszystkich odwiedzających tę stronę, którzy mają ten sam problem z commata, możesz zmienić:
$num = number_format($value, 1, ',', '');
Do
$num = str_replace(',0', '', number_format($value, 1, ',', '')); // e.g. 100,0 becomes 100
To mi pomogło i chyba nie jestem sama.
Jeśli do usunięcia są dwa zera:
Do
$num = str_replace(',00', '', number_format($value, 2, ',', '')); // e.g. 100,00 becomes 100
Więcej tutaj: liczba PHP: punkt dziesiętny widoczny tylko w razie potrzeby
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:26:07
Jeśli chcesz usunąć cyfry zerowe tuż przed wyświetlaniem na stronie lub szablonie.
Możesz użyć funkcji sprintf ()
sprintf('%g','125.00');
//125
sprintf('%g','966.70');
//966.7
sprintf('%g',844.011);
//844.011
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-11 20:58:04
Wpisz do float
.
$int = 4.324000;
$int = (float) $int;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-03 13:25:41
Skomplikowany sposób, ale działa:
$num = '125.0100';
$index = $num[strlen($num)-1];
$i = strlen($num)-1;
while($index == '0') {
if ($num[$i] == '0') {
$num[$i] = '';
$i--;
}
$index = $num[$i];
}
//remove dot if no numbers exist after dot
$explode = explode('.', $num);
if (isset($explode[1]) && intval($explode[1]) <= 0) {
$num = intval($explode[0]);
}
echo $num; //125.01
Powyższe rozwiązania są optymalne, ale jeśli chcesz mieć własne, możesz to wykorzystać. Co ten algorytm robi zaczyna się na końcu łańcucha i sprawdza czy jego 0, jeżeli jest to ustawia na empty string i następnie przechodzi do następnego znaku z powrotem aż do ostatniego znaku jest > 0
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-25 23:26:39
Dziwne, kiedy wyciągam numer z bazy danych o typie "float" i czy mój numer jest ex. 10000 kiedy pływam, staje się 1.
$number = $ad['price_month']; // 1000 from the database with a float type
echo floatval($number);
Result : 1
Przetestowałem wszystkie powyższe rozwiązania, ale nie zadziałały.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-24 07:01:26
Ze względu na to pytanie jest stare. Po pierwsze, przepraszam za to.
Pytanie dotyczy liczby xxx. xx, ale w przypadku, gdy jest to X, xxx.xxxxx lub separator dziesiętny różnicy, taki jak xxxx, xxxx, może to być trudniejsze do znalezienia i usunięcia cyfr zerowych z wartości dziesiętnej.
/**
* Remove zero digits from decimal value.
*
* @param string|int|float $number The number can be any format, any where use in the world such as 123, 1,234.56, 1234.56789, 12.345,67, -98,765.43
* @param string The decimal separator. You have to set this parameter to exactly what it is. For example: in Europe it is mostly use "," instead of ".".
* @return string Return removed zero digits from decimal value.
*/
function removeZeroDigitsFromDecimal($number, $decimal_sep = '.')
{
$explode_num = explode($decimal_sep, $number);
if (is_array($explode_num) && isset($explode_num[count($explode_num)-1]) && intval($explode_num[count($explode_num)-1]) === 0) {
unset($explode_num[count($explode_num)-1]);
$number = implode($decimal_sep, $explode_num);
}
unset($explode_num);
return (string) $number;
}
A oto kod do testu.
$numbers = [
1234,// 1234
-1234,// -1234
'12,345.67890',// 12,345.67890
'-12,345,678.901234',// -12,345,678.901234
'12345.000000',// 12345
'-12345.000000',// -12345
'12,345.000000',// 12,345
'-12,345.000000000',// -12,345
];
foreach ($numbers as $number) {
var_dump(removeZeroDigitsFromDecimal($number));
}
echo '<hr>'."\n\n\n";
$numbers = [
1234,// 12324
-1234,// -1234
'12.345,67890',// 12.345,67890
'-12.345.678,901234',// -12.345.678,901234
'12345,000000',// 12345
'-12345,000000',// -12345
'12.345,000000',// 12.345
'-12.345,000000000',// -12.345
'-12.345,000000,000',// -12.345,000000 STRANGE!! but also work.
];
foreach ($numbers as $number) {
var_dump(removeZeroDigitsFromDecimal($number, ','));
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-18 08:22:59
Możesz użyć:
print (floatval)(number_format( $Value), 2 ) );
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-20 08:07:10
$str = 15.00;
$str2 = 14.70;
echo rtrim(rtrim(strval($str), "0"), "."); //15
echo rtrim(rtrim(strval($str2), "0"), "."); //14.7
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-23 15:00:50
To moje małe rozwiązanie... Może być dołączony do klasy i zestawu var
Private $dsepparator = '.'; / / dziesiętne private $tsepparator=','; / / thousand
Które można ustawić przez konstruktor i zmienić na users lang.
class foo
{
private $dsepparator;
private $tsepparator;
function __construct(){
$langDatas = ['en' => ['dsepparator' => '.', 'tsepparator' => ','], 'de' => ['dsepparator' => ',', 'tsepparator' => '.']];
$usersLang = 'de'; // set iso code of lang from user
$this->dsepparator = $langDatas[$usersLang]['dsepparator'];
$this->tsepparator = $langDatas[$usersLang]['tsepparator'];
}
public function numberOmat($amount, $decimals = 2, $hideByZero = false)
{
return ( $hideByZero === true AND ($amount-floor($amount)) <= 0 ) ? number_format($amount, 0, $this->dsepparator, $this->tsepparator) : number_format($amount, $decimals, $this->dsepparator, $this->tsepparator);
}
/*
* $bar = new foo();
* $bar->numberOmat('5.1234', 2, true); // returns: 5,12
* $bar->numberOmat('5', 2); // returns: 5,00
* $bar->numberOmat('5.00', 2, true); // returns: 5
*/
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-31 08:03:10
Uważaj na dodawanie +0.
echo number_format(1500.00, 2,".",",")+0;
//1
Wynik to 1.
echo floatval('1,000.00');
// 1
echo floatval('1000.00');
//1000
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-19 15:18:10
To jest moje rozwiązanie. Chcę zachować możliwość dodawania separatora tysięcy
$precision = 5;
$number = round($number, $precision);
$decimals = strlen(substr(strrchr($number, '.'), 1));
return number_format($number, $precision, '.', ',');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-11 16:16:16
Ten kod usunie zero po punkcie i zwróci tylko dwie cyfry dziesiętne.
$ Liczba=1200.0000;
str_replace (".00',", number_format($number, 2,'.', ''));
Wyjście będzie: 1200
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-03 08:58:38
$value = preg_replace('~\.0+$~','',$value);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-25 06:30:52