diff --git a/src/sim/money.ts b/src/sim/money.ts new file mode 100644 index 0000000..b21abcb --- /dev/null +++ b/src/sim/money.ts @@ -0,0 +1,18 @@ +import { GameDate, isEuroEra } from './calendar'; + +export const FRANCS_PER_EURO = 6.55957; + +export function francsToCents(francs: number): number { + return Math.round((francs / FRANCS_PER_EURO) * 100); +} + +export function centsToFrancs(cents: number): number { + return (cents / 100) * FRANCS_PER_EURO; +} + +export function formatMoney(cents: number, date: GameDate): string { + const euro = isEuroEra(date); + const value = euro ? cents / 100 : centsToFrancs(cents); + const text = value.toFixed(2).replace('.', ','); + return euro ? `${text} €` : `${text} F`; +} diff --git a/tests/sim/money.test.ts b/tests/sim/money.test.ts new file mode 100644 index 0000000..dd2c165 --- /dev/null +++ b/tests/sim/money.test.ts @@ -0,0 +1,26 @@ +import { describe, it, expect } from 'vitest'; +import { centsToFrancs, formatMoney, francsToCents } from '../../src/sim/money'; + +describe('money', () => { + it('converts francs to euro cents at the fixed rate', () => { + expect(francsToCents(249)).toBe(3796); + expect(francsToCents(199)).toBe(3034); + expect(francsToCents(0)).toBe(0); + }); + + it('converts cents back to francs', () => { + expect(centsToFrancs(3796)).toBeCloseTo(249, 1); + }); + + it('formats in francs before 2002', () => { + expect(formatMoney(3796, { year: 1990, month: 9, day: 28 })).toBe('249,00 F'); + }); + + it('formats in euros from 2002', () => { + expect(formatMoney(3796, { year: 2002, month: 1, day: 1 })).toBe('37,96 €'); + }); + + it('formats negative amounts', () => { + expect(formatMoney(-3796, { year: 2002, month: 1, day: 1 })).toBe('-37,96 €'); + }); +});