Home > Blockchain >  Calculate shipping costs on the basis of product weight
Calculate shipping costs on the basis of product weight

Time:09-02

my math is not so good, but can you guys help me with this

problem statement Suppose I have 4 books with weights and prices.

  1. Book1, 0.5KG
  2. Book2, 0.8KG
  3. Book3, 1KG
  4. Book4, 0.3KG

I have a base price (shipping cost) based on weight, which is 30 Rs Per 0.5KG. Now when I select "book 1", the shipping cost will be 30 Rs, but how can I get the shipping cost for book 2,book 3 and book 4?

CodePudding user response:

it's not related to any programming or algorithm , anyways if

30Rs -> 0.5KG
x    -> 0.8Kg

then simply for Book2

x =  (30Rs*0.8KG)/0.5KG =  48Rs

similarly for book3 and book4:

book3 =  (30Rs*1KG)/0.5KG =  60Rs
book4 =  (30Rs*0.3KG)/0.5KG =  18Rs

another way to solve it is if every 30RS corresponds to 0.5KG then by dividing each side by 5 then 6RS corresponds to 0.1KG.

Book2 is 0.8KG which is 8 times the value 0.1KG then it must cost 8 times the value 6RS so 8 * 6 = 48RS similarly for **Book3 and Book4 where

Book3 = 10 * 6 = 60RS
Book4 = 3 * 6 = 18RS

CodePudding user response:

If the pricing is in brackets, that is the cost for 0 - 0.5kg is 30RS, and 0.5 - 1kg is 60RS, then you have to do as follows:

First find how many brackets you have:

weight / bracketSize

For your books, this will be:

Book1: 0.5/0.5  // 1
Book2: 0.8/0.5  // 1.6
Book3: 1/0.5    // 2
Book4, 0.3/0.5  // 0.6

Then, you need to round that value up to the nearest whole number. How you do this will depend on what language you're using, but it's often called Ceiling or ceil:

Book1: Ceiling(1)    // 1
Book2: Ceiling(1.6)  // 2
Book3: Ceiling(2)    // 2
Book4, Ceiling(0.6)  // 1

Then multiply by price to get your answer.

Book1: 1 * 30    // 30
Book2: 2 * 30    // 60
Book3: 2 * 30    // 60
Book4, 1 * 30    // 30

In one line:

result = Ceiling(weight / bracketSize) * pricePerBracket
  • Related