Two of the most-used calculators in Saudi Arabia have nothing to do with each other on the surface. One tells a buyer what a piece of gold is worth; the other tells an employee what they are owed when they leave a job. Under the surface they are the same program: a small set of inputs, a tiered rule, and a rendered breakdown that shows its working. This is a design walkthrough of both, with the real constants, the edge cases that break naive implementations, and the skeleton they share. You can implement either in any language in an afternoon; the value is in getting the rules right.
Calculator 1: what is this gold worth?
The Saudi gold market has a property that makes it unusually clean to model. The riyal is pegged to the US dollar at 3.7500, so the local price of gold moves only with the world spot price; there is no exchange-rate term. The national per-gram price for 24K is therefore the spot price per troy ounce, converted to riyals at 3.75, divided by 31.1035 grams. Everything else is multiplication.
- Start from the 24K gram price. Fetch the spot price at a fixed interval (a few times a day is enough for a content site; a trading tool needs more), convert it to riyals at the peg, and divide by 31.1035. Cache the result with a timestamp, and show the timestamp.
- Apply the purity multiplier. A karat is a purity out of 24, and Saudi hallmarks express the same thing in parts per thousand. 21K is 875, so its gram price is the 24K price multiplied by 0.875. 22K is 916 (strictly 0.9167), 18K is 750, 14K is 585. Store the multipliers as the hallmark numbers divided by 1,000, not as karats divided by 24, so the table on the page and the constant in the code agree.
- Apply the weight and the unit. Shops price by the gram, but buyers from South Asia think in tolas (11.6638 grams) and international buyers in troy ounces (31.1035 grams). Convert the input to grams once, at the boundary, and do all arithmetic in grams.
- Then, and only then, add the shop's charges. The making charge is a percentage or a per-gram fee for workmanship, and VAT at 15 percent applies to the metal and the making charge together for jewellery, while investment bars of 99 percent purity are zero-rated. The order matters: metal value, plus making charge, then VAT on the sum.
A gold price calculator for the Saudi market takes the live 24K gram price, multiplies by the purity of the karat (0.875 for 21K) and by weight in grams, tolas or ounces, and only then adds the making charge and 15 percent VAT. The rounding rule is the part people get wrong: round the displayed per-gram rate to two decimals for the reader, but compute the total from the unrounded rate, or a 100-gram bar will be off by a riyal and a buyer will notice.
| Karat | Hallmark | Multiplier | Gram price at 24K = SAR 520 | Tola (11.6638 g) |
|---|---|---|---|---|
| 24K | 999 | 0.999 | SAR 520.00 | SAR 6,065.18 |
| 22K | 916 | 0.916 | SAR 476.32 | SAR 5,555.71 |
| 21K | 875 | 0.875 | SAR 455.00 | SAR 5,307.03 |
| 18K | 750 | 0.750 | SAR 390.00 | SAR 4,548.88 |
| 14K | 585 | 0.585 | SAR 304.20 | SAR 3,548.13 |
Note the first row: 24K jewellery is hallmarked 999, not 1000, so a strict implementation multiplies by 0.999 and shows SAR 519.48 rather than 520.00. Decide which convention your page uses and state it, because the two answers differ by half a riyal a gram and readers compare pages.
Calculator 2: what am I owed when I leave?
End-of-service benefits under the Saudi Labor Law are a tiered accrual with a second rule layered on top, which is exactly the shape that produces wrong answers when written from memory.
- The accrual (Article 84). Half a month's wage for each of the first five years of service, then a full month's wage for each year after that. Partial years count proportionally, so compute the service length from exact dates in days, not in whole years.
- The resignation reduction (Article 85). If the employee resigns rather than being terminated, the award is reduced: nothing for under two years of service, one third of the award between two and five years, two thirds between five and ten years, and the full award at ten years or more. Termination by the employer pays the full award regardless of length, subject to the other articles.
- The wage base. The law's definition of wage includes fixed, regular allowances such as housing in most readings, and excludes variable commissions by agreement under Article 86. Let the user build the wage from components rather than typing one number, and show which components were included.
This end of service calculator applies the Article 84 accrual (half a month's wage per year for the first five years, a full month for every year after) and the Article 85 resignation reductions, with partial years counted by exact dates. The edge case worth a test is the person who resigns at four years and eleven months: the accrual is computed on 4.92 years, then reduced to one third, and the difference between that and waiting a month is a large sum of money, which is the reason people use the calculator in the first place.
| Scenario: SAR 10,000 monthly wage | Accrual (Article 84) | Resignation factor (Article 85) | Payout |
|---|---|---|---|
| Terminated after 3 years | 3 x 0.5 x 10,000 = 15,000 | Not applicable | SAR 15,000 |
| Resigns after 3 years | 15,000 | One third | SAR 5,000 |
| Resigns after 7 years | (5 x 0.5 + 2 x 1) x 10,000 = 45,000 | Two thirds | SAR 30,000 |
| Resigns after 12 years | (5 x 0.5 + 7 x 1) x 10,000 = 95,000 | Full | SAR 95,000 |
| Resigns after 1 year 11 months | 9,583 | Nothing under two years | SAR 0 |
The skeleton they share
- Inputs at the boundary, normalised once. Weight to grams; dates to days; wage to a monthly figure.
- The rule as pure arithmetic, with every constant named after the thing it is (the hallmark, the article number), so a reader can check it against the source.
- A rendered breakdown, not just a total. The gold page shows metal, making charge and VAT as three lines; the gratuity page shows the accrual, the factor and the result. People trust a number they can see being built.
- A visible timestamp or version. The gold rate changes several times a day; the labour law changes rarely but does. Say when the rule was last checked.
When you find yourself building a third calculator of this kind, pull those four points into a small reusable structure and stop there. Neither of these needs a framework, and both are more useful than most of the software written with one.
Constants as published by the Saudi Labor Law (Articles 84 to 86) and the national gold market; example prices are illustrative and checked September 2026.
