Skip to content

Usage & Examples

Real melt-value calculations using gold-melt-value. Every example below compiles against the published crate; spot prices use a sample $3983.30/oz.

Setup

# Cargo.toml
[dependencies]
gold-melt-value = "0.1"

Example 1 — A 14k gold chain weighed in grams

The most common case: a piece of jewelry weighed on a gram scale, marked 14k.

use gold_melt_value::{melt_value_grams, purity};

fn main() {
    let spot_per_oz = 3983.30;   // today's gold spot, USD per troy ounce
    let chain_grams = 10.0;       // a 10 g chain
    let karat = 14;

    let melt = melt_value_grams(chain_grams, karat, spot_per_oz);
    println!("14k purity:       {:.4}", purity(karat));     // 0.5833
    println!("Melt value:       ${melt:.2}");               // $747.05
}

Output:

14k purity:       0.5833
Melt value:       $747.05

Example 2 — Pennyweight (US scrap / coin dealers)

Many US gold buyers quote and weigh in pennyweight (dwt). Use melt_value_dwt to avoid a manual unit conversion.

use gold_melt_value::melt_value_dwt;

fn main() {
    let spot_per_oz = 3983.30;
    let dwt = 8.0;                 // 8 pennyweight of 18k
    let melt = melt_value_dwt(dwt, 18, spot_per_oz);
    println!("Melt value: ${melt:.2}");
}

dwt ↔ troy ounces

20 dwt = 1 troy oz exactly. So melt_value_dwt(20.0, 24, s) equals the full spot price of one pure ounce.

Example 3 — Bullion measured in troy ounces

For bars and coins already denominated in troy ounces, melt_value_troy_oz skips the gram conversion entirely.

use gold_melt_value::melt_value_troy_oz;

fn main() {
    let spot = 3983.30;

    // A 1/2 oz 22k gold coin (e.g. close to a Krugerrand's purity)
    let coin = melt_value_troy_oz(0.5, 22, spot);
    println!("Coin melt value: ${coin:.2}");
}

Example 4 — Compare purities side by side

Quick table showing how melt value scales with karat for a fixed 5 g weight.

use gold_melt_value::{melt_value_grams, karats};

fn main() {
    let spot = 3983.30;
    let grams = 5.0;

    for (name, k) in [("24k", karats::K24), ("22k", karats::K22),
                      ("18k", karats::K18), ("14k", karats::K14),
                      ("10k", karats::K10)] {
        let v = melt_value_grams(grams, k, spot);
        println!("{name}: ${v:.2}");
    }
}

Output:

24k: $640.33
22k: $587.02
18k: $480.25
14k: $373.52
10k: $266.79

Example 5 — Silver works too

The crate is metal-agnostic: pass any spot price and the math is identical. For silver the per-gram numbers are far smaller, so mind your decimal precision.

use gold_melt_value::melt_value_grams;

fn main() {
    // Silver at $48.40/oz, a 25 g .999 (24k-equivalent purity) bar
    let silver_melt = melt_value_grams(25.0, 24, 48.40);
    println!("Silver melt value: ${silver_melt:.2}");   // ~$38.90
}

Choosing a function

You have the weight in… Use
grams melt_value_grams
pennyweight (dwt) melt_value_dwt
troy ounces melt_value_troy_oz
only need purity ratio purity
per-gram spot price spot_per_gram

Live, always-fresh calculator

These examples use a hardcoded spot price for reproducibility. For a real-time melt value with today's spot price already pulled in, use the free gold melt-value calculator{: target="_blank" rel="dofollow"} — it runs this exact engine in the browser, across all karats and weight units.