Skip to main content
Get started with PPP pricing in just a few minutes. This guide will walk you through your first PPP calculation and show you practical examples.

Your first PPP calculation

After installing the package, import and use the ppp function:
import ppp from '@sachithrrra/ppp';

// Calculate a fair price for a $10 product in Sri Lanka
const fairPrice = ppp(10, 'LK');
console.log(fairPrice); // Output: 4.3108...

Understanding the parameters

The ppp function accepts four parameters to give you full control over pricing:
1

originalPrice (required)

The base price in USD. This is typically your US market price.
const originalPrice = 10; // $10 USD
2

countryCode (required)

ISO 2-letter or 3-letter country code. Case-insensitive.
ppp(10, 'LK');  // Sri Lanka (2-letter)
ppp(10, 'LKA'); // Sri Lanka (3-letter)
ppp(10, 'lk');  // Also works (case-insensitive)
3

smoothing (optional, default: 0.2)

Controls discount aggressiveness. Range: 0 to 1.
  • 0 = Maximum discount (raw PPP)
  • 0.2 = Balanced (recommended default)
  • 1 = No discount (original price)
ppp(10, 'LK', 0);   // 2.87 - Aggressive discount
ppp(10, 'LK', 0.2); // 4.31 - Balanced
ppp(10, 'LK', 1);   // 10.00 - No discount
4

rounding (optional, default: 'none')

How to format the final price. Options: 'none', 'currency', 'pretty'.
ppp(10, 'LK', 0.2, 'none');     // 4.3108... - Full precision
ppp(10, 'LK', 0.2, 'currency'); // 4.31 - Standard currency
ppp(10, 'LK', 0.2, 'pretty');   // 4.49 - Marketing friendly

Rounding strategies

Choose the right rounding strategy for your use case:
// Best for: Internal calculations, chaining with other math
const price = ppp(10, 'LK', 0.2, 'none');
console.log(price); // 4.310847...

Real-world example: SaaS pricing

Here’s how to implement PPP pricing for a SaaS product with multiple tiers:
import ppp from '@sachithrrra/ppp';

// Your standard USD pricing tiers
const pricingTiers = {
  basic: 9,
  pro: 29,
  enterprise: 99
};

// User's country from IP geolocation or request headers
const userCountry = 'IN'; // India

// Calculate adjusted prices with pretty rounding for display
const adjustedPricing = {
  basic: ppp(pricingTiers.basic, userCountry, 0.2, 'pretty'),
  pro: ppp(pricingTiers.pro, userCountry, 0.2, 'pretty'),
  enterprise: ppp(pricingTiers.enterprise, userCountry, 0.2, 'pretty')
};

console.log(adjustedPricing);
// Output:
// {
//   basic: 2.49,      // Instead of $9
//   pro: 7.49,        // Instead of $29
//   enterprise: 25    // Instead of $99
// }

Error handling

The function returns null for invalid country codes and throws errors for invalid parameters:
import ppp from '@sachithrrra/ppp';

// Invalid country code returns null
const result = ppp(10, 'INVALID');
console.log(result); // null

// Invalid parameters throw errors
try {
  ppp(-10, 'US'); // Negative price
} catch (error) {
  console.error(error.message);
  // "Original price must be a non-negative number."
}

try {
  ppp(10, 'US', 2); // Smoothing out of range (0-1)
} catch (error) {
  console.error(error.message);
  // "Smoothing must be a number between 0 and 1."
}

try {
  ppp(10, 'US', 0.2, 'invalid'); // Invalid rounding strategy
} catch (error) {
  console.error(error.message);
  // "Rounding must be one of: 'none', 'currency', 'pretty'."
}
Always validate country codes and handle null returns gracefully in production code. Consider using a fallback to your standard pricing when PPP data is unavailable.

Next steps

Core Concepts

Learn how PPP calculations work under the hood

API Reference

Explore the complete API documentation

Best Practices

Optimize your PPP pricing strategy

Use Cases

See real-world implementation examples

Build docs developers (and LLMs) love