mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-15 11:42:15 +01:00
add support for interval / duration format in frontend views
This commit is contained in:
41
resources/js/packages/ui/src/utils/number.ts
Normal file
41
resources/js/packages/ui/src/utils/number.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
export type NumberFormat =
|
||||
| 'point-comma'
|
||||
| 'comma-point'
|
||||
| 'space-comma'
|
||||
| 'space-point'
|
||||
| 'apostrophe-point';
|
||||
|
||||
/**
|
||||
* Formats a number according to the specified format
|
||||
* @param value - The number to format
|
||||
* @param format - The format to use
|
||||
* @returns The formatted number as a string
|
||||
*/
|
||||
export function formatNumber(value: number, format?: string): string {
|
||||
// Convert to fixed 2 decimal places first
|
||||
const parts = value.toFixed(2).split('.');
|
||||
const wholePart = parts[0];
|
||||
const decimalPart = parts[1];
|
||||
|
||||
// Format the whole number part based on the format
|
||||
let formattedWhole: string;
|
||||
switch (format) {
|
||||
case 'point-comma':
|
||||
formattedWhole = wholePart.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
|
||||
return `${formattedWhole},${decimalPart}`;
|
||||
case 'comma-point':
|
||||
formattedWhole = wholePart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
||||
return `${formattedWhole}.${decimalPart}`;
|
||||
case 'space-comma':
|
||||
formattedWhole = wholePart.replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
|
||||
return `${formattedWhole},${decimalPart}`;
|
||||
case 'space-point':
|
||||
formattedWhole = wholePart.replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
|
||||
return `${formattedWhole}.${decimalPart}`;
|
||||
case 'apostrophe-point':
|
||||
formattedWhole = wholePart.replace(/\B(?=(\d{3})+(?!\d))/g, "'");
|
||||
return `${formattedWhole}.${decimalPart}`;
|
||||
default:
|
||||
return value.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user