Skip to content

Commit 9b3f277

Browse files
ed-snodgrassEd Snodgrassdavidjgoss
authored
#332 rounding down of passed percentage (#380)
Co-authored-by: Ed Snodgrass <[email protected]> Co-authored-by: David Goss <[email protected]>
1 parent 7cc7430 commit 9b3f277

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
### Fixed
1010
- Make keyword spacing look right ([#376](https://github.com/cucumber/react-components/pull/376))
1111
- Fix issue with hook steps not being rendered ([#379](https://github.com/cucumber/react-components/pull/379))
12+
- Rounding down of percent passed and adding one decimal place ([#380](https://github.com/cucumber/react-components/pull/380))
1213

1314
### Changed
1415
- Inherit font-size instead of setting at 14px ([#377](https://github.com/cucumber/react-components/pull/377))

src/formatStatusRate.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import { formatStatusRate } from './formatStatusRate.js'
44

55
describe('formatStatusRate', () => {
66
const examples: [passed: number, total: number, percentage: string][] = [
7-
[13, 45, '29%'],
8-
[5, 45, '11%'],
7+
[13, 45, '28.8%'],
8+
[5, 45, '11.1%'],
99
[45, 45, '100%'],
1010
[0, 45, '0%'],
1111
[0, 0, '0%'],
12+
[999, 1000, '99.9%'],
13+
[99999, 100000, '99.9%'],
14+
[9999999, 10000000, '99.9%'],
1215
]
1316

1417
for (const [passed, total, percentage] of examples) {

src/formatStatusRate.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export function formatStatusRate(passed: number, total: number) {
2+
const percentPassed = total > 0 ? passed / total : 0
3+
const roundedDown = Math.floor(percentPassed * 1000) / 1000
4+
25
return new Intl.NumberFormat(undefined, {
36
style: 'percent',
4-
}).format(total > 0 ? passed / total : 0)
7+
maximumFractionDigits: 1,
8+
}).format(roundedDown)
59
}

0 commit comments

Comments
 (0)