Skip to content
Advertisement

How to get upto two digits after decimal in PHP

Sample Input

-33.8670522

151.1977362

12.9582505

Required Output

-33.86

151.19

12.95

Output that i am getting

-33.87

151.20

12.96

Below is my code but issue is it is rounding off the last digit

JavaScript

Can anybody help me with it!!!!

Advertisement

Answer

To get your desired results, multiply the numbers by 100, take the intval to truncate to the integer part and then divide by 100 again:

JavaScript

Output:

JavaScript

Demo on 3v4l.org

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement