php remove trailing zeros of a decimal number

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


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 thoughts on “php remove trailing zeros of a decimal number”

Leave a Comment