Trailing Zeros and Decimals

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ILoveJackDaniels
Forum Commoner
Posts: 43
Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK

Trailing Zeros and Decimals

Post 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?
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post 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
User avatar
ILoveJackDaniels
Forum Commoner
Posts: 43
Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try this:

Code: Select all

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