0

I have a cart price where I have to split items into multiple checks and want to divide quantity by the split and but show the fraction number into ratio for the end user.

Here is the example code.

$product = 'Cheese Pizza';
$quantity = 1;
$split = 3;

There are 3 checks and each check should have the number like below

// Check 1
1/3 Cheese Pizza (the quantity is stored in the database is .33)

I was trying this by the following code.

$product_name =  $quantity."/".$split"    ".$product;

However if I have quantity divided into let's say 3, I have fraction in the quantity.

So it looks like I need fraction to ratio. Any ideas?

Amit Patel
  • 75
  • 3
  • paxdiablo, thanks so much.... I always dont have 1 as fixed number.. I can get any decimal number.. so I am thinking is it really even possible to convert any fraction number to ratio.. for example if I have an item with 7 quantity divided into 10 pieces... so would I get 7/10 from the fraction number.. any other suggestions. – Amit Patel Apr 27 '15 at 18:20

1 Answers1

0

I'm not totally certain I understand the question but, if you're asking how to turn your 0.33 into a proper fraction, it's simply (pseudo-code):

fpval = 0.33
denon = round (1 / fpval)
string = "1/" + to_string(denom)

This works because 1/(1/3) is equal to 3.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953