Fixed an error where toFixed() wouldn't round correctly. Added a util function that does a better job (at least for 2 digits precision)

This commit is contained in:
2025-10-29 10:24:39 +01:00
parent da73a18bf9
commit 039ffa6f2e
3 changed files with 10 additions and 6 deletions
@@ -2,7 +2,7 @@
import { computed } from 'vue' import { computed } from 'vue'
import { type Invoice } from '@/types' import { type Invoice } from '@/types'
import { toLocalDate, toCurrency } from '@/lib/utils' import { toLocalDate, toCurrency, toFixedRounded } from '@/lib/utils'
import { StatusBadge, statusBadgeLabels, statusBadgeTextColor, castToStatusVariant } from '@/components/ui/status-badge' import { StatusBadge, statusBadgeLabels, statusBadgeTextColor, castToStatusVariant } from '@/components/ui/status-badge'
import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from '@/components/ui/table' import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from '@/components/ui/table'
@@ -97,7 +97,7 @@ const totalGross = computed(() => {
}) })
const calcTaxes = (amount: number) => { const calcTaxes = (amount: number) => {
return Number((0.19 * amount).toFixed(2)) return toFixedRounded(Number(0.19 * amount), 2)
} }
</script> </script>
@@ -12,7 +12,7 @@
import { ref, computed, watch, onMounted, onUpdated, useTemplateRef } from "vue" import { ref, computed, watch, onMounted, onUpdated, useTemplateRef } from "vue"
import { Customer, Invoice, Contact, PaymentTerms, Address } from "@/types" import { Customer, Invoice, Contact, PaymentTerms, Address } from "@/types"
import { newCustomer, newContact, newBillingData } from '@/types/index.d' import { newCustomer, newContact, newBillingData } from '@/types/index.d'
import { toCurrency, toLocalDate, toShortISOString, cn, calcDueDate } from '@/lib/utils'; import { toCurrency, toLocalDate, toShortISOString, cn, calcDueDate, toFixedRounded } from '@/lib/utils';
import axios from 'axios' import axios from 'axios'
import { type DateValue, DateFormatter, getLocalTimeZone, parseDate, fromDate } from "@internationalized/date" import { type DateValue, DateFormatter, getLocalTimeZone, parseDate, fromDate } from "@internationalized/date"
import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"
@@ -383,8 +383,7 @@ const updateTotalAmount = () => {
<div class="flex flex-col m-0 items-end gap-0"> <div class="flex flex-col m-0 items-end gap-0">
<span class="text-md text-muted-foreground">{{ toCurrency(invoice.totalAmount) }}</span> <span class="text-md text-muted-foreground">{{ toCurrency(invoice.totalAmount) }}</span>
<span class="text-2xl font-bold">{{ toCurrency(Number((invoice.totalAmount * <span class="text-2xl font-bold">{{ toCurrency(toFixedRounded(Number(invoice.totalAmount * 1.19), 2)) }}</span>
1.19).toFixed(2))) }}</span>
</div> </div>
</div> </div>
+6 -1
View File
@@ -7,13 +7,14 @@ export function cn(...inputs: ClassValue[]) {
} }
export function urlIsActive(urlToCheck: NonNullable<InertiaLinkProps['href']>, currentUrl: string) { export function urlIsActive(urlToCheck: NonNullable<InertiaLinkProps['href']>, currentUrl: string) {
return toUrl(urlToCheck) === currentUrl; return currentUrl.indexOf(toUrl(urlToCheck)) >= 0
} }
export function toUrl(href: NonNullable<InertiaLinkProps['href']>) { export function toUrl(href: NonNullable<InertiaLinkProps['href']>) {
return typeof href === 'string' ? href : href?.url; return typeof href === 'string' ? href : href?.url;
} }
const currencyFormatter = new Intl.NumberFormat('de-DE', { const currencyFormatter = new Intl.NumberFormat('de-DE', {
style: 'currency', style: 'currency',
currency: 'EUR', currency: 'EUR',
@@ -25,6 +26,10 @@ export function toCurrency(value: number | undefined) {
return currencyFormatter.format(value); return currencyFormatter.format(value);
} }
export function toFixedRounded(num: number, precision: number): number {
return Number((Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision)).toFixed(precision))
}
export function toLocalDate(value: string) { export function toLocalDate(value: string) {
const date = new Date(value); const date = new Date(value);
return date.toLocaleDateString('de-DE', { day: '2-digit', month: 'short', year: 'numeric' }); return date.toLocaleDateString('de-DE', { day: '2-digit', month: 'short', year: 'numeric' });