Skip to content

Commit 02c6ce7

Browse files
authored
Update how our util functions round numbers (#1865)
* Use toFixed to handle rounding * more precise comment * Round it inline as well, just to be sure
1 parent ad2ea54 commit 02c6ce7

4 files changed

Lines changed: 7 additions & 6 deletions

File tree

app/pages/system/UtilizationPage.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
Table,
2525
Tabs,
2626
} from '@oxide/ui'
27-
import { bytesToGiB, bytesToTiB } from '@oxide/util'
27+
import { bytesToGiB, bytesToTiB, round } from '@oxide/util'
2828

2929
import { CapacityBars } from 'app/components/CapacityBars'
3030
import { useDateTimeRangePicker } from 'app/components/form'
@@ -254,7 +254,8 @@ const AvailableCell = ({
254254
return (
255255
<div className="flex w-full items-center justify-between">
256256
<div>
257-
{allocated - provisioned} {unit && <span className="text-tertiary">{unit}</span>}
257+
{round(allocated - provisioned, 2)}
258+
{unit && <span className="text-tertiary"> {unit}</span>}
258259
</div>
259260
{/* We only show the ResourceMeter if the percent crosses the warning threshold (66%) */}
260261
{usagePercent > 66 && (

app/test/e2e/utilization.e2e.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ test.describe('System utilization', () => {
2121

2222
const table = page.getByRole('table')
2323
await expectRowVisible(table, {
24-
CPU: '20 ',
24+
CPU: '20',
2525
Storage: '2.7 TiB',
2626
Memory: '66 GiB',
2727
Silo: 'maze-war',
2828
})
2929
await expectRowVisible(table, {
30-
CPU: '26 ',
30+
CPU: '26',
3131
Storage: '7 TiB',
3232
Memory: '350 GiB',
3333
Silo: 'myriad',

libs/util/math.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ it('rounds properly', () => {
1616
expect(round(123.456, 2)).toEqual(123.46)
1717
expect(round(123.456, 3)).toEqual(123.456)
1818
expect(round(123.456, 4)).toEqual(123.456) // trailing zeros are culled
19+
expect(round(123.0001, 3)).toEqual(123) // period is culled if decimals are all zeros
1920
expect(round(1.9, 0)).toEqual(2)
2021
expect(round(1.9, 1)).toEqual(1.9)
2122
expect(round(5 / 2, 2)).toEqual(2.5) // math expressions are resolved

libs/util/math.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ export function splitDecimal(value: number) {
1616
}
1717

1818
export function round(num: number, digits: number) {
19-
const pow10 = Math.pow(10, digits)
20-
return Math.round((num + Number.EPSILON) * pow10) / pow10
19+
return Number(num.toFixed(digits))
2120
}

0 commit comments

Comments
 (0)