Rommeo Posted June 16, 2010 Share Posted June 16, 2010 In my DB, I have 'price(DEC--6,3)' attribute; Example Values : 10.000 12.250 8.125 And I want an output like : 10 12.25 8.125 I want to remove zeros at the end after the dot, Is there any easy way to do this ? Link to comment https://forums.phpfreaks.com/topic/204984-any-function-for-this-purpose/ Share on other sites More sharing options...
Alex Posted June 16, 2010 Share Posted June 16, 2010 You could use rtrim. Link to comment https://forums.phpfreaks.com/topic/204984-any-function-for-this-purpose/#findComment-1073141 Share on other sites More sharing options...
Rommeo Posted June 16, 2010 Author Share Posted June 16, 2010 I used $num1=rtrim($num1,"0"); and the problem is, it gives me result like this : num1 :1 num2 :12.25 num3 :8.125 Link to comment https://forums.phpfreaks.com/topic/204984-any-function-for-this-purpose/#findComment-1073142 Share on other sites More sharing options...
Alex Posted June 16, 2010 Share Posted June 16, 2010 There might be a better solution but you could do something like this: if(strpos($num1, '.') !== false) { $num1 = rtrim($num1, '0'); } Link to comment https://forums.phpfreaks.com/topic/204984-any-function-for-this-purpose/#findComment-1073143 Share on other sites More sharing options...
Rommeo Posted June 17, 2010 Author Share Posted June 17, 2010 Now the result : num1: 10. num2: 12.25 num3: 8.125 Link to comment https://forums.phpfreaks.com/topic/204984-any-function-for-this-purpose/#findComment-1073277 Share on other sites More sharing options...
Alex Posted June 17, 2010 Share Posted June 17, 2010 function stripzeros($num) { if(strpos($num, '.') !== false) { $split = explode('.', $num); if($split[1] == 0) { return $split[1]; } return $split[0] . '.' . rtrim($split[1], '0'); } return $num; } Link to comment https://forums.phpfreaks.com/topic/204984-any-function-for-this-purpose/#findComment-1073278 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.