Jump to content

Can php convert these fractions?


Scooby08

Recommended Posts

I was just wondering if php can convert a fraction that I'm getting from an xml feed..

 

The fractions are like so: 4½

 

Otherwise I was thinking of doing a str_replace like so:

 

str_replace('½','.5',$fraction);

 

But that gave me another problem.. It comes out like so:

 

4�.5

 

Any suggestions how I could just get 4.5?

 

Thanks!!

Link to comment
Share on other sites

to convert a fraction simply divide the numerator by the denominator n/d:

1/2 would be 1 divided by 2. format it to 2 decimal places.

 

Look at the source and see how the fraction is generated. If its ascii only 1/4,1/2 and 3/4 are available and you would just have to convert the symbol. If there are other fractions then probable just explode it on the slash and divide. if its a graphic and does not have alt description then YOYO.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Not talking about having control of the output, just what the raw code is.

<fraction>4 3/4</fraction>
<fraction 4 ¾</fraction>
<fraction>4 &#190;</fraction>

will all display 4 3/4 but are not the same and wont be handled the same, code wise, when you have to work on converting them.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

I did that and then I ran into this problem:

 

4�.5

 

I did happen to get this one to finally work though..

 

preg_replace('/[^A-Za-z0-9]+$/','.5',$string);

 

That one removed all characters that weren't letters or numbers, including that hidden question mark character.. Then it just replaced the ½ at the end.. In my case I will only have ½ if there are fractions, so I can just replace all those with .5..

 

Thanks for helping out on this one teamatomic!! I really appreciate it..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.