Menu

Converting Spot Gold (USD/oz) to Saudi Riyals per Gram by Karat in JavaScript
πŸ“°

Converting Spot Gold (USD/oz) to Saudi Riyals per Gram by Karat in JavaScript

DEV Community: javascriptΒ·TechGuide HQΒ·4 days ago
#EWKrXJDg
#devtojavascript#devto#dev#karat#price
Long article (544 words) β€” no AI summary yet.
Reading 0:00
15s threshold

Gold prices are quoted globally in US dollars per troy ounce, but shoppers in Saudi Arabia think in riyals per gram, split by karat (24K, 22K, 21K, 18K). If you are building a finance widget, a price bot or just a personal dashboard, converting between the two is a small but surprisingly error-prone task. Here is a clean way to do it.

The constants you need

1 troy ounce = 31.1034768 grams. Not the 28.35 g avoirdupois ounce β€” this is the most common bug.

USD/SAR = 3.75. The Saudi riyal has been pegged to the dollar at this rate for decades, so you can usually hard-code it (but keep it configurable).

Purity by karat is simply karat / 24 . So 21K is 0.875 pure, 18K is 0.75 pure.

The helper

const GRAMS_PER_TROY_OZ = 31.1034768 ; const USD_TO_SAR = 3.75 ;

function goldPricePerGramSAR ( spotUsdPerOz , karat = 24 , { usdToSar = USD_TO_SAR } = { } ) { if ( ! Number .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.