PHP removes a decimal number from a number string

I have an algorithm that returns a number with a decimal point (IE ".57"). What I would like to do is just get “57” without a decimal.

I used eregi_replace and str_replace and it doesn't work!

$one = ".57";
$two = eregi_replace(".", "", $one);
print $two;
+3
source share
6 answers
$one = '.57';
$two = str_replace('.', '', $one);
echo $two;

It works. 100% verified. BTW, all functions of ereg (i) _ * are depreciated. Use preg_ * if you need a regular expression.

+7
source
Method       Result   Command
x100         57       ((float)$one * 100))
pow/strlen   57       ((float)$one * pow(10,(strlen($one)-1))))
substr       57       substr($one,1))
trim         57       ltrim($one,'.'))
str_replace  57       str_replace('.','',$one))

Just combining some other methods to get the same result.

+3
source
$number = ltrim($number, '.');

.

+2

, str_replace , :

<?php
    $one = '.57';
    $two = str_replace('.', '', $one);
    echo $two;
?>
+1

eregi_replace str_replace !

Well ... ereg_replace()it won’t work, because it uses regular expressions, and the character .(period) has a special meaning: everything (so you replaced each character with "" (empty line)).

But it str_replace()works absolutely fine in this case.

Here is a live test: http://ideone.com/xKG7s

+1
source

It returned as an integer.

$two = (integer) trim('.', $one);
0
source

Source: https://habr.com/ru/post/1786467/


All Articles