string - php strip leading zeros and 0 decimal values -


this falls category of 'i'm sure there cleaner way this' although works well. maybe kind of function.

i have lists of values such 01.0, 09.5, 10.0, 11.5,

i want values exclude leading 0 , keep decimal portion if it contains .5. there never other decimal value. current code is:

$data = '09.5'; //just example value  if (substr($data,0,1) == '0' ) {     $data = substr($data, 1); } if (stripos($data, '.0') !== false ) {     $data = str_replace('.0','',$data); } print $data; 

just cast float:

$data = '09.5'; echo (float) $data; // 9.5  $data = '09.0'; echo (float) $data; // 9  $data = '010'; echo (float) $data;  // 10 

demo

you can use floatval()

echo floatval($data); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -