Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP remove decimal from number string

Tags:

php

decimal

eregi

i have an algorithym that gives back a number with a decimal point (I.E. ".57"). What I would like to do is just get the "57" without the decimal.

I have used eregi_replace and str_replace and neither worked!

$one = ".57";
$two = eregi_replace(".", "", $one);
print $two;
like image 382
DonJuma Avatar asked Dec 04 '22 09:12

DonJuma


2 Answers

$one = '.57';
$two = str_replace('.', '', $one);
echo $two;

That works. 100% tested. BTW, all ereg(i)_* functions are depreciated. Use preg_* instead if you need regex.

like image 58
Jonah Avatar answered Dec 27 '22 07:12

Jonah


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 shwoing some other methods of getting the same result

like image 28
Brad Christie Avatar answered Dec 27 '22 08:12

Brad Christie