troubleshooting Question

Preg_Replace Help

Avatar of Sailo100
Sailo100 asked on
7 Comments1 Solution321 ViewsLast Modified:
I am using a number which can have up to 8 decimal places.  However this number is actually currency so i would like it to display clearly.

I need to make sure i have no less than 2 decimal places but display anywhere up to 8.

For example i need these numbers to display these results:

0.2 = 0.20
0.1 = 0.1
0.15 = 0.15
0.156800 = 0.156800
0.268 = 0.268
1.95660010 = 1.9566001

I am currently using this code which works fine but it will not leave me with 2 decimal places if the last number is a 0.

      $longDecimal = number_format($old_number, 8, '.', '');
      $number = preg_replace('/\.?0+$/', '', $longDecimal);

Does anybody no the preg_replace pattern for only replacing chars after 2 decimal places??
Jason Minton

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros