I have a function that rounds each percentage to 1:
items() {
return this.data
.map(item => {
const perc= item.count/ number * 100
const percentage = parseFloat(perc).toFixed(1) + '%'
return { ...item, percentage }
})
.sort((a, b) => b.count - a.count)
}
But the problem is that it rounds also the fixed numbers. For example:
100% => 100.0% -> issue
So how can I fix this?
2
Answers
Just check if it is a whole number before you round:
Try something like this:
(Note that
perc
already is a number, so you don’t needparseFloat()
You can easily achieve it with numeraljs
So your code will look like this