Page 1 of 1

Trailing Zeros and Decimals

Posted: Sun Jul 20, 2003 3:15 am
by ILoveJackDaniels
I really need to remove the trailing zeroes from a set of usersubmitable numbers. The problem is that the user may submit numbers with 0, 1 or 2 decimal places. If they only use one decimal place, the number looks like 10.20 - and I need to strip out that trailing zero. I can't do it with number_format, because I don't know how many trailing zeros I need to split out of each number. Can anyone help?

Posted: Sun Jul 20, 2003 3:35 am
by Fredix
you could search the number for a dot " . "
if the number is 10 you don't do anything

but if there is a dot - the number could be 18.900
you start a loop that reads from right to left and looks whether the number is == 0 if so you delete it and do this until the number you are looking at is != 0

hope i helped

Posted: Sun Jul 20, 2003 4:14 am
by ILoveJackDaniels
That's pretty much what I'd done so far - I was hoping there was a function to do it without all the looping - all the functions I've seen that could do this so far require a specific number of decimal places to be specified.

Posted: Mon Jul 21, 2003 3:47 am
by twigletmac
Try this:

Code: Select all

$test = '12.3400000';
echo $test.'<br />';
$stripped_zeros = (float)$test;
echo $stripped_zeros;
Mac