Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove trailing zeroes 1

Status
Not open for further replies.

rrsub

MIS
Oct 23, 2002
536
US
Is there a function that I can't find that will remove trailing zeroes from a number with a decimal?

I can't find one so I'm creating one now

 
look at the printf options. %.2d or something like that should do what you need.
 
Thanks ericbrunson for pointing me but this is exactly what I wanted.
Code:
<?PHP
function cutzero($value) 
{
   return preg_replace("/(\.\d+?)0+$/", "$1", $value)*1;
}
?>

 
The PHP manual recommends not using regular expression functions where not necessary. The regular expression functions are resource-intensive.

Code:
<?php

function cutzero ($value)
{
	return (int) $value;
}

$a = 12345.000000;

print cutzero ($a);
?>



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
The regular expression does give me decimal places which I need.

I'm working with page sizes and when I convert points to inches, I get values like 8.5000000068 which I only need 8.5, not 8.50 or 8.500. There are times I will need 6.125 or 6.15 but not 6.150. If I get 11.000, I only want 11 and not "11."

 
Would it be better to have a loop that removed the trailing zero instead of regular expressions?

 
I can't way which of a loop or a regular expression would be less resource-intensive. But I'd guess the loop.

What's wrong number_format()?

Code:
<?php
$a = 8.5000000068;
$b = number_format ($a, 1);

print $b;
?>

Outputs:

8.5

Just keep in mind that number_format() rounds. Had $a been equal to 8.560000, my script would have output "8.6".


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I use number_format to get the 3 decimal places.

8.124669 is fine as "8.125"

What I don't want is "8.500" but "8.5"
If I get "8.125" I need "8.125" not "8.1"
If I get "6.250" I need "6.25" not "6.250" or "6.3"
If I get "11.000" I want "11" not "11.000"

Is there a way I can test the stress the difference of the loop vs a regex?

 
Try rtrim():

Code:
<?php
print '<pre>';
$a = array (8.500, 8.125, 6.250, 11.000);

foreach ($a as $b)
{
	print rtrim ($b, '.0');
	print "\n";
}
?>

Outputs:

Code:
8.5
8.125
6.25
11

If you want to test a loop against a regex, write two scripts and compare runtimes using microtime()


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
In the interest of preciseness, if we go back to the original question... you don't have a decimal, you have a string representation of a decimal. Removing the zeros from a string is a completely different problem than removing the zeros from a number.
 
Either way to fix the problem didn't really matter to me. It seems logical to handle it as a number instead of a string.

But, another problem crept up;

If the number is "10", 1 is returned.

I'll work on that tomorrow.

 
what if you search for "." in reverse, with strrpos() and do a trim on that?

eg:

Code:
$decimals = 3;
$blah = substr(strrpos($blah, ".") + $decimals, strlen($blah));

ps. this code might be wrong, I have not tested it.
There may also be some performance issues that I am not aware of, but try the microtime() as suggested above, to test what is the fastest way to go!

good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top