Files
accounted/lib/bokslut/arsredovisning/pdf-format.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

24 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Whole-krona amount formatting for text that react-pdf renders with the
* built-in Helvetica font (the K2/K3 arsredovisning PDFs and their notes).
*
* `toLocaleString('sv-SE')` formats negative numbers with U+2212 MINUS SIGN,
* which WinAnsi-encoded standard fonts cannot encode; react-pdf drops the
* glyph silently, so a loss like -4 684 printed as "−4 684" loses its
* sign and reads as a profit. Format the absolute value and prepend an ASCII
* hyphen-minus instead. The sv-SE thousands separator (U+00A0 NBSP) is in
* WinAnsi and renders fine.
*/
const NBSP = String.fromCharCode(0x00a0)
export function formatPdfKronor(amount: number): string {
const rounded = Math.round(amount)
// Pin the group separator to U+00A0: depending on the Node/ICU CLDR
// version, sv-SE groups with U+00A0 or U+202F NARROW NO-BREAK SPACE, and
// U+202F is missing from WinAnsi too (digits would silently run together).
const grouped = Math.abs(rounded)
.toLocaleString('sv-SE')
.replace(/\s/g, NBSP)
return rounded < 0 ? `-${grouped}` : grouped
}