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,}$/", "&frac12;", $number); } elseif(preg_match("/^[0]*\.5[0]{0,}$/", $number)) {     echo $used = str_replace("$number", "&frac12;", $number); } else {     echo $number; } 

output: 10½


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -