numberUtils
Reference
Utility functions for numeric formatting.
const { protrakUtils } = React.useContext(customWidgetContext);
const { round } = protrakUtils;
round(number, decimalPlaces?)
Rounds a number to the specified number of decimal places using a precision-safe algorithm (avoids floating-point rounding errors).
Parameters:
| Param | Type | Default | Description |
|---|---|---|---|
number | number | required | The number to round |
decimalPlaces | number | 2 | Number of decimal places |
Returns: number
round(3.14159); // → 3.14
round(3.14159, 3); // → 3.142
round(2.005, 2); // → 2.01 (precision-safe, not 2.00)
round(100); // → 100
round(-1.555, 2); // → -1.56
Usage Example
function CostSummaryWidget(pageContext) {
const { protrakUtils, protrakComponents } =
React.useContext(customWidgetContext);
const { useProtrakApi, round } = protrakUtils;
const { Box, H2, Text, Spinner } = protrakComponents;
const { state } = useProtrakApi({
requestConfig: fetchLineItems,
instanceId: pageContext.instanceId,
});
if (state.isLoading) return <Spinner />;
const items = state.data?.items ?? [];
const total = items.reduce((sum, item) => {
return (
sum +
(item.attributes?.find((a) => a.name === 'UnitCost')?.numericValue ?? 0)
);
}, 0);
return (
<Box style={{ padding: '1rem' }}>
<H2>Cost Summary</H2>
<Text>Total: ${round(total, 2).toLocaleString()}</Text>
</Box>
);
}
const fetchLineItems = ({ instanceId }) => ({
endpoint: `instances/${instanceId}/relatedInstances`,
config: {
method: 'GET',
params: {
typeName: 'LineItem',
attributes: ['UnitCost'],
skip: 0,
take: 999,
},
},
});