Skip to content
Financial Calculations logo

Financial Calculations

Decimal precision and financial math patterns in LivestockAI

SKILL.md

Full skill instructions

Financial Calculations

LivestockAI uses decimal.js for precise financial calculations. All monetary values are stored as DECIMAL(19,2) in the database.

Currency Utilities

Located in app/​features/​settings/​currency.ts:

import {
  toDecimal,
  toNumber,
  toDbString,
  formatCurrency,
  multiply,
  divide,
  sumAmounts,
} from '~/​features/​settings/​currency'

Core Functions

Converting Values

// From any input to Decimal
const amount = toDecimal('1234.56')
const amount2 = toDecimal(1234.56)

// To number for display
const num = toNumber('1234.56') // 1234.56

// To string for database
const dbVal = toDbString(1234.56) // "1234.56"

Arithmetic Operations

// Multiplication (quantity × price)
const total = multiply(100, 5.5) // Decimal(550)

// Division (with null safety)
const perUnit = divide(total, 100) // Decimal(5.50) or null

// Subtraction
const profit = subtract(revenue, costs)

// Sum multiple amounts
const totalCost = sumAmounts(feedCost, laborCost, overheadCost)

Formatting

// Format for display (uses user settings)
formatCurrency(1234.56) // "₦1,234.56" or "$1,234.56"

// Compact format
formatCurrencyCompact(1500000) // "₦1.5M"

// Percentage
calculatePercentage(50, 200) // 25

React Hook

In components, use the useFormatCurrency hook:

import { useFormatCurrency } from '~/​features/​settings'

function PriceDisplay({ amount }: { amount: number }) {
  const { format, symbol } = useFormatCurrency()

  return <span>{format(amount)}</​span> // "₦1,234.56"
}

Database Storage

All monetary values are stored as DECIMAL(19,2):

// Inserting
await db.insertInto('sales').values({
  totalAmount: toDbString(1234.56), // "1234.56"
})

// Reading
const sale = await db.selectFrom('sales').executeTakeFirst()
const amount = toNumber(sale.totalAmount) // 1234.56

Business Logic Examples

Batch Total Cost

export function calculateBatchTotalCost(
  initialQuantity: number,
  costPerUnit: number,
): string {
  if (initialQuantity <= 0 || costPerUnit < 0) {
    return toDbString(0)
  }
  return toDbString(multiply(initialQuantity, costPerUnit))
}

Profit Calculation

export function calculateProfit(
  revenue: MoneyInput,
  costs: MoneyInput,
): Decimal {
  return subtract(revenue, costs)
}

Related Skills

  • multi-currency - Currency settings and formatting
  • three-layer-architecture - Service layer calculations

More Sales & marketing skills

revops logo
Sales & marketing

revops

Design and optimize systems connecting marketing, sales, and customer success into a unified revenue engine.

53.3K 104.9K
View
lead-magnets logo
Sales & marketing

lead-magnets

Strategic planning and optimization for email capture offers and gated content.

53.3K 103.4K
View
directory-submissions logo
Sales & marketing

directory-submissions

Directory submissions: the foundation layer of distribution that passes backlinks and discovery surface area into your marketing pages.

53.3K 76.3K
View
gsap-timeline logo
Sales & marketing

gsap-timeline

Sequence and choreograph multi-step animations with precise timing control and nested timelines.

15.9K 58.4K
View
launch-strategy logo
Sales & marketing

launch-strategy

Structured framework for planning product launches, feature announcements, and go-to-market strategies.

53.3K 56.8K
View
gws-drive-upload logo
Sales & marketing

gws-drive-upload

Upload files to Google Drive with automatic MIME type detection and metadata.

31.2K 47.2K
View
ci-cd-and-automation logo
Sales & marketing

ci-cd-and-automation

Automates CI/CD pipeline setup. Use when setting up or modifying build and deployment pipelines. Use when you need to automate quality gates, configure test runners in CI, or establish deployment strategies.

101.4K 43.2K
View
persona-team-lead logo
Sales & marketing

persona-team-lead

Team leadership persona with standup coordination, meeting prep, task delegation, and team communication.

31.2K 29.3K
View
recipe-find-large-files logo
Sales & marketing

recipe-find-large-files

Identify large Google Drive files consuming storage quota.

31.2K 29.2K
View

Sales & marketing AI tools

KreadoAI logo
Sales & marketing

KreadoAI

Revolutionize Video Creation with AI-Generated Avatars and Voices

Paid
View
Followr logo
Sales & marketing

Followr

Social media on autopilot

Paid
View
Logodiffusion logo
Sales & marketing

Logodiffusion

Design custom logos instantly with AI.

Paid
View
I
Sales & marketing

Ideogram

Text-heavy marketing and design images

Freemium
View
Prompt Silo logo
Sales & marketing

Prompt Silo

Boost Your SEO with Content and SEO Silos

Free
View
Marketing Blocks logo
Sales & marketing

Marketing Blocks

Revolutionize Your Marketing with AI-Powered MarketingBlocks!

Paid
View
Scrip AI logo
Sales & marketing

Scrip AI

10X faster & free AI content creation for 100+ use cases

Free
View
Tribescaler logo
Sales & marketing

Tribescaler

Tribescaler | Make Your Tweets Go Viral

Paid
View