0

Consider the below code

$t = preg_replace('/0+$/','',".800000000000000"); //Replace last 0s

This gives me output as .8 as expected

Now consider the below code

$a = .80;
$t = sprintf('%.15f', $a)."<br>";
echo "First : $t<br>";
$t = preg_replace('/0+$/','',$t);
echo "Second : $t <br>";

This gives output as First : 0.800000000000000 Second : 0.800000000000000

Could you help me to find out why the last 0s are not replaced by regular expression in this case as the expected output is 0.8 ?

3 Answers 3

4

Since you are adding <br> to the end of $t with this line:

$t = sprintf('%.15f', $a)."<br>";

You regex no longer matches trailing 0. "<br>" is part of the presentation, you should add it at the very end.

Sign up to request clarification or add additional context in comments.

1 Comment

oh! Sorry, I could not notice this. Thanks a lot.
3

You append a <br> tag at the end, while the Regex says 0's before end of line

'/0+$/'
    ^ <- end of line, nothing should come after 0's
".800000000000000"

$a = .80;
$t = sprintf('%.15f', $a)."<br>";
// $t = .800000000000000<br>

Comments

1

your variable $t contains 0.800000000000000<br> so there ar no trailing zeros to cut off.

you'll have to shorten the string before appending <br>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.