Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove trailing zeros until 2 decimals in PHP

Tags:

php

I've tried casting to float and number_format but float will always round at two and number_format is fixed on the amount of decimals you specify.

So how can I do this like the following conversion

11.2200 -> 11.22
11.2000 -> 11.20
11.2340 -> 11.234
like image 549
Bart Avatar asked Dec 13 '25 06:12

Bart


1 Answers

$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"

This might help, You can use sprintf given by PHP.

like image 195
Tilak Raj Avatar answered Dec 15 '25 19:12

Tilak Raj