# how to calculate fare based on kilometers?

Page 1 of 1

## 10 Replies - 289 Views - Last Post: 12 February 2019 - 10:19 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=414879&amp;s=b8b5884fda69be01add4bf9c308247f6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 victor_ma2k10

Reputation: 0
• 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 {

}

}

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

• 痛覚残留

Reputation: 4223
• 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.

### #3 victor_ma2k10

Reputation: 0
• 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

### #4 Dormilich

• 痛覚残留

Reputation: 4223
• 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.

### #5 victor_ma2k10

Reputation: 0
• 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

### #6 Dormilich

• 痛覚残留

Reputation: 4223
• 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?

### #7 victor_ma2k10

Reputation: 0
• 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

### #8 modi123_1

• Suitor #2

Reputation: 14862
• 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.

### #9 victor_ma2k10

Reputation: 0
• 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

### #10 modi123_1

• Suitor #2

Reputation: 14862
• 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.

### #11 ArtificialSoldier

• D.I.C Lover

Reputation: 2241
• 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?