1

Actually, "rounding" may not be the best name to describe what I'm looking to do.

I want to round only floats that have zeros after the decimal point: ".00"

I don't want to display any decimal numbers if there are zeros after the decimal point. Normally I'd just look for ".00" in the variable, and if it has what I'm looking for I just strip it out. However, there are a few instances that the decimal might be longer than 2 places. like: 45.000 or 56.0000

I didn't want to mess with any regex on this if there was a php function that stripped out the extra zeros, if they exist.

2
  • 1
    What about "4.50"? Should that become "4.5"? Commented May 17, 2011 at 20:34
  • Clarification: I am assuming that you are trying to read in a value, then write the value with as few trailing 0s as possible. Is this correct? Commented May 17, 2011 at 21:29

2 Answers 2

2

Using floatval() might be the solution to your problem.

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

1 Comment

This does it. Seems I need to convert the string to an int or float. I didn't think that it was in a string and needed conversion.
0

Use round($f,2);
http://php.net/manual/en/function.round.php

For that genius who downvoted: http://codepad.org/2bBA5UpL

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.