php - Convert .5 into 1/2 -
i want convert number ends in .5 displays number followed ½, don't want 0.5 display 0½ did this:
$used = str_replace("0.5", "½", $used); $used = str_replace(".5", "½", $used);
however i've realised converts 20.5 2½ instead of 20½.
i'm sure there's better way of doing don't know how.
examples:
5 returns "5" 5.5 returns "5½" 0.5 returns "½" 10.5 returns "10½"
i don't believe duplicate of existing question because code replace or return "1/2" rather "½"
php code demo(in html work fine)
<?php $number="10.5000"; if(preg_match("/^[1-9][0-9]*\.5[0]{0,}$/", $number)) { echo $used = preg_replace("/\.5[0]{0,}$/", "½", $number); } elseif(preg_match("/^[0]*\.5[0]{0,}$/", $number)) { echo $used = str_replace("$number", "½", $number); } else { echo $number; }
output: 10½
Comments
Post a Comment