• Status: Solved
• Priority: Medium
• Security: Public
• Views: 630

# removing trailing zeros from decimal

i am dealing with very small nubers up to 8 decimal places in some, bt some are only 3 decimals and i need to show them all.

What is the easiest way to remove the trailing zeros for some numbers
i.e. 0.00456000 becomes 0.00456 but if the number is 0.04567890 it becomes 0.0456789 i do not want to round off just remove zeros from the end

I am using

printf("%0.8f", \$total_base);

to format the numbers from my equations other wise they come scientific

1E-2556

thanks in advance for any and all help
0
mrwilde
• 2
1 Solution

Commented:
Hi mrwilde,

one way this can be done is
for (\$i = 0; \$i < 8; \$i++)
{
if (round(\$total_base,\$i) == \$total_base)
{
printf("%0." . \$i . "f", \$total_base);
}
}

cachedVB
0

Commented:
Watch this :)

function rtrim_zeros (\$num) { return rtrim (sprintf("%0.8f", \$num), "0"); }
0

Commented:
Example:

<?
\$total_base = 0.004560000;
echo rtrim_zeros (\$total_base);
echo "<br>";

\$total_base = 0.04567890;
echo rtrim_zeros (\$total_base);
echo "<br>";

function rtrim_zeros (\$num) { return rtrim (sprintf("%0.8f", \$num), "0"); }
?>

--
peyox
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.