php remove trailing zeros of a decimal number

Method to remove or strip trailing zeros of a decimal number:

<?php
function clean_num( $num ){
	$pos = strpos($num, '.');
	if($pos === false) { // it is integer number
		return $num;
	}else{ // it is decimal number
		return rtrim(rtrim($num, '0'), '.');
	}
}
?>

Works on numbers: 0.02=0.02; 4.000=4; 80=80.

2 comments on “php remove trailing zeros of a decimal number

  1. Hannes

    With little addition works even with 1.000.00,00 Nice idea with the rtrim(rtrim($num, '0'), '.');

Leave a Reply

Your email address will not be published. Required fields are marked *