Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to separate last digits in Php?

I have a string value which size 11(2): 00000700000

I want to convert this string to decimal. I clean leading zeros with ltrim function and then put comma at the end of the value with the function substr_replace:

ltrim(substr_replace( '00000700000', '.', -2, 0), '0')

The expected result is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

7000.00

Are there any cleaner way to separate last two digits with dot and convert this string to decimal?

>Solution :

You can simply cast that string to int and divide by 100:

intval('00000700000') / 100; // 7000

If you want that same number formatted, use format_number():

number_format(num: $result, decimals: 2, thousands_separator: '') // 7000.00

Example

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading