-2

I want to convert any number which ends in .5 so that it displays as the number followed by ½, but I don't want 0.5 to display as 0½ so I did it like this:

$used = str_replace("0.5", "½", $used);
$used = str_replace(".5", "½", $used);

However I've now realised that this also converts 20.5 into 2½ instead of 20½.

I'm sure there's a better way of doing it but I don't know how.

Examples:

5 returns "5"
5.5 returns "5½"
0.5 returns "½"
10.5 returns "10½"

I don't believe this is a duplicate of an existing question because that code is to replace or return "1/2" rather than "½"

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Andy Groom
  • 619
  • 1
  • 7
  • 15

2 Answers2

0

Based on the examples above and lacking any further requirements, you could write:

<?php

  $n = "13.5";
  /* ... */
  $r = $n;
  $r = preg_replace ('/^0\.5$/', '&frac12;', $r);
  $r = preg_replace ('/\.5$/', '&frac12;', $r);

  echo "$r\n";

You can combine the above into a single replacement:

  $r = preg_replace ('/(^0|)\.5$/', '&frac12;', $n);
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
-1

PHP code demo(In HTML it will 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½

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • 1
    This doesn't seem to work for me, is it because your sample entry ends in a zero (ie. like currency) whereas my numbers are unformatted. My values will be like 10.5 rather than 10.50 – Andy Groom Apr 06 '17 at 12:01
  • @AndyGroom It will work fine for that as well you can check. I have updated my post keeping all the requirements in mind, specified by you. – Sahil Gulati Apr 06 '17 at 12:03
  • *"I don't want 0.5 to display as 0½"*. What happens if `$number` is "0.5"? – JJJ Apr 06 '17 at 12:04
  • @JJJ then it just returns ½ – Andy Groom Apr 06 '17 at 12:08
  • @Sahilgulati my mistake sorry – Andy Groom Apr 06 '17 at 12:08
  • @AndyGroom This code does *not* return just "½". – JJJ Apr 06 '17 at 12:08
  • 1
    The body of the second branch can be simplified to: `echo "½";`. – Nisse Engström Apr 06 '17 at 17:25
  • @NisseEngström I have updated my post, can you please check again. If still there is any issue, can you let me know – Sahil Gulati Apr 07 '17 at 07:01
  • @JJJ I have updated my post, can you please check again. If still there is any issue, can you let me know – Sahil Gulati Apr 07 '17 at 07:01
  • 1
    I didn't downvote, but this is extremely convoluted for a simple task. `if($number=="0.5") { $number="½"; } else { $number=str_replace(".5", "½", $number);}` – JJJ Apr 07 '17 at 07:08
  • @JJJ What you are saying is correct but there can be a case when we have a number like this `10.5000` then it will return like this `10½000` which is invalid. that's why i have handled it this way. But i dont know why people are downvoting :( – Sahil Gulati Apr 07 '17 at 07:15
  • You don't know if there can be such a case. The OP didn't say anything like that, it's possible that it's guaranteed that there are no trailing zeroes. – JJJ Apr 07 '17 at 07:18
  • Even if there can be trailing zeroes this could be done with one line of regex. – JJJ Apr 07 '17 at 07:20
  • @JJJ Sorry to say sir, I am not arguing just take a case when number is `10.566` – Sahil Gulati Apr 07 '17 at 07:20