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
https://forums.phpfreaks.com/topic/189253-can-php-convert-these-fractions/
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

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

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..

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.