10 Replies - 289 Views - Last Post: 12 February 2019 - 10:19 AM Rate Topic: -----

#1 victor_ma2k10   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 19

how to calculate fare based on kilometers?

Posted 12 February 2019 - 07:05 AM

Hi.

I am trying to calculate fare based on kilometers, for example minimum fare is 20 for 2km or below and if 2.1km fare should be 20.5 or if 3km fare is 25. how to achieve this problem?

My current code below is converted kilometers to meters. missing part is if 2.1km and above.

Do I need my base is 0.05?

$distance_km = 3;

echo calculateFare(kilometersToMeters($distance_km));

function calculateFare($distance) {

	if ($distance < 2000 or $distance <= 2000) {

		return $cost = 20;

	} else {

		// no answer here

	}

}


function kilometersToMeters($km){

    return $km * 1000;

}



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: how to calculate fare based on kilometers?

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4223
  • View blog
  • Posts: 13,380
  • Joined: 08-June 10

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 07:20 AM

you actually have two separate problems here. One is the unmodified fare (i.e. price per km multiplied by actual distance) and the other is the minimum fare per distance category. The latter is essentially just a couple of if()s.
Was This Post Helpful? 0
  • +
  • -

#3 victor_ma2k10   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 19

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 07:33 AM

Do you have suggestion to solve the problem?

pricing format is:

2km and below = 20
2.1km = 20.5
2.2km = 21
3km = 25
3.3 = 26.5
4km = 30
4.1km = 30.5
...

This post has been edited by Dormilich: 12 February 2019 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4223
  • View blog
  • Posts: 13,380
  • Joined: 08-June 10

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 07:44 AM

If you look at the data you (should) realise that these prices increase linearly with the distance /1/. In other words, you can use a simple formula to calculate the price (no need for if()s at all).

/1/ - if you don't see it, use a spreadsheet program to visualise price versus distance.
Was This Post Helpful? 0
  • +
  • -

#5 victor_ma2k10   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 19

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 07:54 AM

I don't understand :( can you help with basic example. thanks!

This post has been edited by Dormilich: 12 February 2019 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4223
  • View blog
  • Posts: 13,380
  • Joined: 08-June 10

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 08:15 AM

you know Excel?
Was This Post Helpful? 0
  • +
  • -

#7 victor_ma2k10   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 19

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 08:43 AM

I use Google Sheet but just for entering columns and rows.

This post has been edited by Dormilich: 12 February 2019 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14862
  • View blog
  • Posts: 59,297
  • Joined: 12-June 08

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 08:50 AM

Then you should see that .1 increase in one column translates to .5 increase in the other column.
Was This Post Helpful? 0
  • +
  • -

#9 victor_ma2k10   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-February 19

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 09:02 AM

column a - 0.1
columb b - 0.5’and increase?

This post has been edited by Dormilich: 13 February 2019 - 01:58 AM

Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14862
  • View blog
  • Posts: 59,297
  • Joined: 12-June 08

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 09:02 AM

I don't get what you wrote.
Was This Post Helpful? 0
  • +
  • -

#11 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2241
  • View blog
  • Posts: 6,796
  • Joined: 15-January 14

Re: how to calculate fare based on kilometers?

Posted 12 February 2019 - 10:19 AM

Subtract 2 from the total distance and 20 from the total price, and list the distances and prices. There's a pattern that you need to see. 1km = how much price?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1