feat(sim): franc/euro money helpers
This commit is contained in:
1 parent
b6054086c3
commit
cb103bf71a
2 files changed
+44
No files matched your search
@@ -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`;
|
||||
}
|
||||
@@ -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 €');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user